using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerMover : MonoBehaviour { public KeyCode up = KeyCode.W; public KeyCode down = KeyCode.S; public KeyCode left = KeyCode.A; public KeyCode right = KeyCode.D; public Rigidbody2D rb; public PlayerStats playerStats; public float speed = 2f; // Start is called before the first frame update void Start() { rb = GetComponent(); playerStats = GetComponent(); speed = playerStats.speed; } // Update is called once per frame void Update() { Move(); } public void Move(){ Vector2 moveDirection = Vector2.zero; if(Input.GetKey(up)){ moveDirection += Vector2.up; } if(Input.GetKey(down)){ moveDirection += Vector2.down; } if(Input.GetKey(left)){ moveDirection += Vector2.left; } if(Input.GetKey(right)){ moveDirection += Vector2.right; } if(moveDirection.magnitude > 1){ moveDirection.Normalize(); // Normalize if diagonal to avoid faster movement } rb.velocity = moveDirection * speed; } }