public class Movement : MonoBehaviour
{
Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Space)) ;
rb.AddForce(Vector3.up * 4f);
if (Input.GetKey(KeyCode.W))
rb.AddForce(Vector3.forward * 2f);
if (Input.GetKey(KeyCode.D))
rb.AddForce(Vector3.right * 2f);
if (Input.GetKey(KeyCode.A))
rb.AddForce(Vector3.left * 2f);
if (Input.GetKey(KeyCode.S))
rb.AddForce(Vector3.back * 2f);
}
}
You mean the player flies up when you hit the space bar?
That’s because you check if space bar is down, not if it went down this frame, use GetKeyDown.
also, not a good thing control a physics body like that, there’s a lot to it to explain.
and when doing movement and alike in Update you need to multiply your values by Time.deltaTime to make them independent of the frame rate, right now your player will change speed with relation to the frame rate.
if you want to move x units per second(not how a rigidbody controller like you did works) you need to say (x * deltaTime), if you just use x you move x every frame.
You also have semi-colon on the first line here when you should not:
if (Input.GetKey(KeyCode.Space)) ;
rb.AddForce(Vector3.up * 4f);
Look at the rest of your if statements and see the difference.
So the if statement is basically doing nothing, and the following line is being run every Update regardless, so you are always adding for to the rigidbody each update.