- Home /
How to change bouncing direction
I have a boucing ball with rigidbody2d and a physics material with bounciness set to 1 how can make the ball go faster up/down when boucing between 90 and 80 degress to avoid being stuck in the tunnel or going up/down too slow? (also need the same thing when bouncing at the horizontal)
You can use the dot product on the ball’s direction vector and the normal vector of the wall to find the angle between them. When the angle is almost the same apply a different force to the ball.
Answer by Twinklier · Dec 31, 2019 at 03:03 AM
Bouncing is a physics stuff, and I doubt there is any settings menu dedicated to bouncing balls. Better implement something!
So my idea is that when the ball hits the wall, instead of just bouncing back, a random force is added backwards with a slight offset. We gunna need a script attached to this ball.
First obvious one is collision detect
void OnCollisionEnter(Collision collision)
{
GetComponent<RigidBody>.AddForce(-collision.contacts[0].normal +
new Vector2(Random.Range(-1, 1), Random.Range(-1, 1)));
}
This chunk of code gets the first contact point, look at the direction it's being hit, and addforce to it at the opposite direction. To spice things up, some random vector is added to give it more...UMPH of a bouncy ball. You can change that random range thing to anything you wish really.
This should do it. Cheers!
Edit: mispelled the code there a bit
take this image as example its not my game but my game is similar! if I let the ball always bounce to a random direction it wont be possible to aim and control the ball direction also it can very often slow down the ball speed or stop the ball completely or speed it up too much the problem is that in my game when the ball hit the corners of any object sometimes it bounces back at 90 degrees or near it, then the ball wont move anymore or move too slow like the X image in my question
Then have it only randomize the bouncing direction when it approaches the 80 - 90 degree threshold.
I used this and the other answer from @$$anonymous$$arioooo and made some checks so the ball wont go towards the same wall and now its working
Answer by Marioooo · Dec 31, 2019 at 11:46 PM
I got into this problem once... What I did is to add a bool variable that sets true once the ball hits a wall.
Then I used late update. Late update occurs after all physics have been calculated. So now I know where the ball is going, I can use a if statement in order to change the direction.
Private void lateUpdate()
{
If (checkDir)
{
// IF RIGIDBODY DIRECTION IS HIGHER THAN 80 DEGREES AND LOWER THAN 110
float currentVelocity = Rigidbody2D.velocity.magnitude;
// CALCULATE NEW DIRECTION
Rigidbody2D.velocity = newDirection.normalized * currentVelocity;
}
}
You can use fixed vector2D values to set the new direction. Hope this helps!
I wrote this in my phone so don't just copy and paste the code.
Don't forget to set the checkDir to false and when you calculate the new direction check if you where moving upwards or downwards and use for x velocity $$anonymous$$athf.Abs so you don't have to worry if the ball is going to the left or the right.