Top Down Character Movement – Unity

using UnityEngine;

public class TopDownCharacterController : MonoBehaviour
    // Define movement speed and reference to Rigidbody2D component
    public float movementSpeed = 5f;
    private Rigidbody2D rb;

    void Start()
        // Get reference to Rigidbody2D component
        rb = GetComponent<Rigidbody2D>();

    void Update()
        // Get input from arrow keys or WASD keys
        float horizontalInput = Input.GetAxisRaw("Horizontal");
        float verticalInput = Input.GetAxisRaw("Vertical");

        // Calculate movement direction based on input
        Vector2 movementDirection = new Vector2(horizontalInput, verticalInput).normalized;

        // Move character based on movement direction and speed
        rb.velocity = movementDirection * movementSpeed;

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock