- Home /
Bouncing ball stops when hitting edge of platform
Hi all,
I am having an issue with a bouncing ball game I'm trying to create. The ball can fit through a hole in the platform it's bouncing on (the user can move platforms left and right), but if the ball bounces near to the edge of the platform, it can start bouncing lower and eventually stop.
I'd like the ball to always bounce to the same height regardless of where it hits the platform but I can't seem to figure this one out! Any help greatly appreciated.
Here is a link to a gif showing what is happening at the minute:
Answer by TheEmeraldRuby · Apr 29, 2018 at 07:57 PM
this should work:
void Start()
{
rb = GetComponent<Rigidbody>();
}
void OnCollisionEnter(Collision collision)
{
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
rb.velocity = rb.velocity = new Vector3(rb.velocity.x, 10, rb.velocity.z);
}
since it resets the ball's y velocity every time it lands on the ground and accelerates it the same amount every time. Just make sure the ball has a Rigidbody component.
I hope this helped!
Answer by MattFrewin · Apr 29, 2018 at 08:23 PM
Thanks @TheEmeraldRuby , I tried your code but I can't seem to get it to work right - it makes the ball bounce way above the platform, like it's bouncing on an invisible platform.
I think the answer could be in that OnCollisionEnter method though so I will keep playing around with that.
That might be because it's colliding with another object that's not the ground e.i. a wall. Give the platform you want it to bounce on a certain tag and then add code to check if the ball is colliding with an object with that tag. If so, tell it to execute the code... unless you've fixed the problem. Sorry I'm late!
Your answer
![](https://koobas.hobune.stream/wayback/20220612161733im_/https://answers.unity.com/themes/thub/images/avi.jpg)