Unity 2D Topdown Player Movement Script

 avatar
unknown
csharp
5 months ago
931 B
31
Indexable
using UnityEngine;


/*
This is a player script for simple topdown 2D movement, if you need a 3D script checkout the video on my channel - Gatsby
*/

public class Player : MonoBehaviour
{
    public float moveSpeed = 5f;                  // Movement speed of the player
    private Rigidbody2D rb;                       // Reference to the Rigidbody2D component

    private Vector2 movement;

    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();         // Get the Rigidbody2D component on the player
    }

    private void Update()
    {
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        movement = movement.normalized;
    }

    private void FixedUpdate()
    {
        // Move the player based on input and move speed
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }
}
Editor is loading...
Leave a Comment