- Home /
How do I increase forward velocity of a 2D ball when it bounces?
Hello There, I want to know what commads I would use and how I would use them to increase overall velocity of a 2D object. I have players and obstacles in the world. When the ball bounces off a player, it should all around get incrementally faster (maybe like 1.1 or 1.2 times its current speed) and when it bounces off an obstacle it decreases speed incrementally (maybe 0.8 or 0.9 times its current speed). Thanks!
Answer by logicandchaos · Sep 13, 2021 at 06:00 AM
Something like this should work:
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))//if collision is with player gameObject
{
rb.velocity = rb.velocity * 1.2f;//increase velocity by 1.2 times, try different values
}
}
put this code in a script on your ball.
This is my 386th answer! That was my goal since I learned to program on an IBM 386 26 years ago! Thanks everyone!
Your answer
Follow this Question
Related Questions
How do I get velocity from a collision? 1 Answer
Unity2D Collision is bugged 0 Answers
2D Geometry dash-like ship physics 0 Answers
Is there a way to lock velocity? 3 Answers
ball breaks through walls 1 Answer