- Home /
Ping Pong Ball break infinite loop
I would like to make a pong game where if the two paddles are exactly opposite to each other, the pong ball which hits back and forth the center of the paddle should deflect little bit and move either to the right or to the left side of its original reflection position. Basically how to break free of that infinite loop?
Also i use rigidbody and collider to the ball and adding force to it, which in turn increases the speed of the pong ball. How do i reduce it without removing rigidbody and addforce()? The paddle does not have rigidbody only the ball has rigibody.
You could add a slight offset to the direction of your force. So, ins$$anonymous$$d of reflecting off the paddle at exactly 90 degrees, make it 89. Such a small difference shouldn't be noticeable but it will stop the ball from getting into a loop. I'm not sure of the maths of this off the top of my head, but I hope this gives you an idea of something to try.
Thanks to your idea I was able to achieve it. I added AddForceAtPosition() to achieve it.
Answer by MonoFlauta · Oct 04, 2018 at 05:12 PM
I will suppose it is 2D. You can check if both game objects transform.position.y are equal. If they are, you can add a bit of force in the up direction or down direction. It would be something like:
if(playerOne.transfrom.position.y == playerTwo.transform.position.y)
{
ballRigidbody.AddForce(Vector3.up * forceValue) //forceValue is the value of force you want to add while ballRigidbody is a reference to the rigidbody
}
About the reduction of the speed. You can always regulate the speed to a certain value by doing:
ballRigidBody.velocity = ballRigidBody.velocity.normalized * speedYouWant;
The normalized will make the vector of the speed be 1 and then you multiply that with the speed you desire. More information about the normalized can be found here: https://docs.unity3d.com/ScriptReference/Vector3-normalized.html
Thanks it worked out, but since i was using splines i am not able to feed the transform.positions. The spline follower handles it. I just added AddForceAtPosition() to fix the issue.
Answer by twramyep · Oct 04, 2018 at 04:24 PM
Noobtuts has a good pong tutorial that I'm following Atm. Good chance you'll find your answers there.
Answer by Saiguru · Oct 05, 2018 at 05:18 AM
Hi, Solved it... Thanks for your ideas and suggestions. Since I was using spline follower and each part of my paddle is a separate sprite i had trouble finding a solution. I guess this solved the thing i was trying. Now the X value changes when it hits the same point twice... Since its a pong game the position of the paddles are upside down...
Vector2 direction = collision.transform.position - transform.position;
rb.AddForceAtPosition(new Vector2(direction.normalized.x + 1.5f, direction.normalized.y), collision.contacts[0].point);
rb.velocity = rb.velocity.normalized * 5;
Your answer
Follow this Question
Related Questions
Is this loop infinite? 2 Answers
Infinite loop when I try to generate randomly a 2D dungeon.. 3 Answers
Ping Pong Reflect 2 Answers
Animation Error on Loop or PingPong 0 Answers
Infinite Looping Crash 2 Answers