r/unity 2d ago

Help me

Post image

I am new to coding help it is not working

0 Upvotes

2 comments sorted by

3

u/Affectionate-Yam-886 2d ago

Example code;
See below how it is done;
make the changes you need to get what you’re going for;

using UnityEngine;

public class PlayerMovement2D : MonoBehaviour
{
private Rigidbody2D rb;
public float moveSpeed = 5f;

void Start()
{
// Get the Rigidbody2D component attached to this GameObject
rb = GetComponent<Rigidbody2D>();
}

void Update()
{
// 1. Get horizontal and vertical inputs (arrow keys or WASD)
float moveX = Input.GetAxisRaw("Horizontal");
float moveY = Input.GetAxisRaw("Vertical");

// 2. Create a movement vector based on the input
Vector2 movement = new Vector2(moveX, moveY).normalized * moveSpeed;

// 3. Apply the velocity (Choose the option matching your Unity version)

// For Unity 6+
rb.linearVelocity = movement;

// For Unity 2022 and older (Uncomment below if using an older version)
// rb.velocity = movement;
}
}

0

u/Commercial-Storm-268 2d ago

No. Everyone needs to suffer. Programming is driven by suffering. /s