ludumdare50/Assets/Scripts/PlayerMovement.cs
2022-04-01 23:35:34 -04:00

34 lines
925 B
C#

using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(PlayerInput), typeof(Rigidbody2D))]
public class PlayerMovement : MonoBehaviour {
[SerializeField] PlayerStats playerStats;
PlayerInput playerInput;
Vector2 moveDirection;
void Start() {
playerInput = GetComponent<PlayerInput>();
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 Update() {
transform.position += (Vector3)moveDirection * Time.deltaTime * playerStats.movementSpeed;
}
void OnMove(InputAction.CallbackContext ctx) {
moveDirection = ctx.ReadValue<Vector2>();
if (moveDirection.sqrMagnitude > 1.0f)
moveDirection.Normalize();
}
}