- Home /
Rigidbody velocity disables gravity
Hello,
I have this script that moves a rigidbody along to its local z axis. The problem is that it disables gravity. How can I fix this?
rigidbody.velocity = transform.forward * Input.GetAxis("Vertical") * speed * Time.deltaTime;
Answer by robertbu · Aug 06, 2014 at 07:05 PM
Try this:
var v = transform.forward;
v.y = 0.0;
v.Normalize();
v *= Input.GetAxis("Vertical") * speed * Time.deltaTime;;
v.y = rigidbody.velocity.y;
rigidbody.velocity = v;
Normalize makes the vector have a length of 1.0. Some of what I did here may be unnecessary, but I did not know your game situation. The 'v.y = 0.0' and the 'v.Normalize()' deals with teh situation of your character's forward looking up or down. If I did not Normalize the vector, then you would see a speed change as the character looked up or dwon.
It is actualy a car, that I want it to be affected from gravity, while being able to move
Your answer
Follow this Question
Related Questions
OnMouseDown() tracking bad at 3D box collider 0 Answers
Rigidbody y velocity is stuck on 0, gravity is not turned off. 2 Answers
Mimic Gravity Movement Via Setting Velocity 0 Answers
rigidbody.Velocity stops gravity 2 Answers
Velocity powered rigidbody on a moving platform without parenting. 3 Answers