38 lines
998 B
C#
38 lines
998 B
C#
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<Rigidbody2D>();
|
|
playerInput = GetComponent<PlayerInput>();
|
|
}
|
|
|
|
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<Vector2>();
|
|
if (moveDirection.sqrMagnitude > 1.0f)
|
|
moveDirection.Normalize();
|
|
}
|
|
} |