using UnityEngine; using UnityEngine.InputSystem; [RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))] public class PlayerMovement : MonoBehaviour { [SerializeField] PlayerStats playerStats; Rigidbody2D rigidbody; PlayerInput playerInput; Vector2 moveDirection; void Awake() { rigidbody = GetComponent(); playerInput = GetComponent(); } void Start() { playerInput.actions["Move"].started += OnMove; playerInput.actions["Move"].performed += OnMove; playerInput.actions["Move"].canceled += OnMove; } void OnDestroy() { playerInput.actions["Move"].started -= OnMove; playerInput.actions["Move"].performed -= OnMove; playerInput.actions["Move"].canceled -= OnMove; } void FixedUpdate() { rigidbody.velocity = (Vector3)moveDirection * playerStats.movementSpeed; } void OnMove(InputAction.CallbackContext ctx) { moveDirection = ctx.ReadValue(); if (moveDirection.sqrMagnitude > 1.0f) moveDirection.Normalize(); } }