- Home /
Why does my character keep sliding around? Help! (Rigid Body)
Whatta finna pop?
I have an issue with my game development. You see, I just created a movement script for my character. He is a rigid body object. He only has an empty with a camera parented to him. This is my script:
void Update () {
if (Input.GetKey("w"))
{
rb.AddForce(0, 0, 3000*Time.deltaTime);
}
if (Input.GetKey("s"))
{
rb.AddForce(0, 0, -3000*Time.deltaTime);
}
if (Input.GetKey("a"))
{
rb.AddForce(-3000*Time.deltaTime, 0, 0);
}
if (Input.GetKey("d"))
{
rb.AddForce(3000*Time.deltaTime, 0, 0);
}
}
Again, my issue is that he just keeps on sliding! He slides a lot after I hit a key. What should I do?
Comment
Answer by SwedishDerp · Feb 22, 2018 at 01:19 PM
It's because you don't stop the motion and let the physics stop him for you. A simple solution I would try is to check when the key is being released and when it is released you stop the motion. For example:
If(Input.GetKeyUp(Keycode.W))
{
Here you either stop OR push rigidbody force in the opposite direction and stop when it hits a limit!
}
Hope it helped you! :)