I got a game in progress, its about a ball that is given force to roll over a terrain. And it does this perfectly!
yet i want to stop my ball when i am pressing “space” button.
I am already using rigidbody.sleep() on ball but the brake of the ball is not like a real.
Plz help me about that.
A simple way to implement a brake is to use a force that is proportional to the object’s velocity:-
var brakeFactor: float;
function FixedUpdate() {
if (Input.GetKey("space")) {
rigidbody.AddForce(-brakeFactor * rigidbody.velocity);
}
}
Set the brakeFactor value according to how strong you want the brakes to be.
Although it gives a good braking effect, this technique may not bring the object to a complete halt in some situations (like a steep hill, say). If you want this to happen then you can detect when the velocity is very low and call rigidbody.Sleep. Also, as it stands, the script will slow the object down even in mid air. If the ball can fall or jump then you will also need to check for contact with the ground before putting on the brakes.
I’m not quite sure why you have got the WaitForSeconds in there but you shouldn’t use that in the FixedUpdate function (it’s a regular, timed update so you shouldn’t try to pause it to wait for something to happen). Does it work correctly if you take that out?