using UnityEngine; [RequireComponent(typeof(Rigidbody2D))] public class TopDown2DPlayerMovement : MonoBehaviour { public float moveSpeed = 5f; private Rigidbody2D rb; private Vector2 moveInput; public Animator animator; Vector2 movement; void Start() { rb = GetComponent(); if (rb == null) { Debug.LogError("Rigidbody2D not found on this GameObject!"); } } void Update() { movement.x = Input.GetAxisRaw("Horizontal"); movement.y = Input.GetAxisRaw("Vertical"); moveInput = new Vector2(movement.x, movement.y).normalized; animator.SetFloat("Horizontal", movement.x); animator.SetFloat("Vertical", movement.y); animator.SetFloat("Speed", movement.sqrMagnitude); } void FixedUpdate() { if (rb != null) { rb.MovePosition(rb.position + moveInput * moveSpeed * Time.fixedDeltaTime); } } }