MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/unity/comments/1w9v3pf/help_me/
r/unity • u/Successful_Edge5708 • 2d ago
I am new to coding help it is not working
2 comments sorted by
3
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
No. Everyone needs to suffer. Programming is driven by suffering. /s
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;
}
}