2026-05-14 2D Platformer Game 캐릭터 이동
This commit is contained in:
@@ -2,15 +2,60 @@
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float moveSpeed = 5f;
|
||||
|
||||
[Header("Jump")]
|
||||
[SerializeField] private float jumpForce = 12f;
|
||||
[SerializeField] private Transform groundCheck;
|
||||
[SerializeField] private float groundCheckRadius = 0.1f;
|
||||
[SerializeField] private LayerMask groundLayer;
|
||||
|
||||
private Rigidbody2D rb;
|
||||
private float moveInputX = 0f;
|
||||
private bool isGrounded;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
|
||||
rb = GetComponent<Rigidbody2D>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
private void Start()
|
||||
{
|
||||
|
||||
InputManager.Instance.OnMove_Event += OnMoveInput;
|
||||
InputManager.Instance.OnJump_Event += OnJumpInput;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (InputManager.Instance != null)
|
||||
{
|
||||
InputManager.Instance.OnMove_Event -= OnMoveInput;
|
||||
InputManager.Instance.OnJump_Event -= OnJumpInput;
|
||||
}
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundLayer);
|
||||
rb.linearVelocity = new Vector2(moveInputX * moveSpeed, rb.linearVelocity.y);
|
||||
}
|
||||
|
||||
private void OnMoveInput(Vector2 value)
|
||||
{
|
||||
moveInputX = value.x == 0f ? 0f : Mathf.Sign(value.x);
|
||||
}
|
||||
|
||||
private void OnJumpInput()
|
||||
{
|
||||
if (isGrounded)
|
||||
rb.linearVelocity = new Vector2(rb.linearVelocity.x, jumpForce);
|
||||
}
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
if (groundCheck == null) return;
|
||||
Gizmos.color = isGrounded ? Color.green : Color.red;
|
||||
Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user