Reputation: 818
I have a GameObject with a rigidbody and a BoxCollider that has a 'bouncy' physics material applied to it. I want the object to bounce the same height all the time, similar to a Doodle Jump type game. I set the bounciness of the physics material to 1, which according to several tutorials I have looked at should keep the ball bouncing at the same height. My scene is very basic and there's not much happening, but I can't seem to get it working. Any suggestions to get my object bouncing the same height over and over again?
Upvotes: 2
Views: 1896
Reputation: 470
I know that this answer is to late , but maybe my solution will help someone else . in addition to the solution above which was the key to solving whole the issue , you can put this inside your script
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag.Equals("ground"))
rigidbody.velocity = new Vector2(rigidbody.velocity.x, 0); // set y velocity to zero
rigidbody.AddForce(new Vector2(0, 400)); // some constant force here
}
}
if you only use the answer above you will get an issue when you have two ball with different Y position because there bouncing height will not be the same , my solution to this was resetting y velocity of the balls to 0 and reforce it again to 400 ( as my example)
Upvotes: 0
Reputation: 3234
The bounciness
of your player is combined with the bounciness of the surface he hits. In your player physics material set Bounce Combine
to Maximum
. This way it should work according to the docs, because a bounciness
of 1
means a bounce without any loss of energy and you are now taking the maximum of 1
and something most likely lesser than 1
for other surfaces.
But, for whatever reason your player will now gain a little height with every bounce. I guess it's some rounding error in the engine. You can try fiddling around with a bounciness
of 0.97
or something like that and it may work.
If you can't find the sweet spot or it doesn't behave the same on all devices, you can implement the bounce yourself and just invert the velocity as soon as the player collides with the surface.
Upvotes: 2