Files
WhiteMan_Unity2D/Assets/02_Scripts/Player/PlayerController.cs

62 lines
1.6 KiB
C#

using UnityEngine;
public class PlayerController : MonoBehaviour
{
[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>();
}
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);
}
}