• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Saiguru · Oct 04, 2018 at 12:42 PM · loopcontrolsballinfinitepingpong

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ellie97 · Oct 04, 2018 at 02:58 PM 2
Share

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.

avatar image Saiguru Ellie97 · Oct 05, 2018 at 05:14 AM 0
Share

Thanks to your idea I was able to achieve it. I added AddForceAtPosition() to achieve it.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Saiguru · Oct 05, 2018 at 05:12 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

95 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Ping Pong Reflect 2 Answers

I broke my game with what i think is an infinite loop? 2 Answers

Can the Unity editor break out of an infinite loop in a script? 13 Answers

Game random freeze / hang up / stuck 0 Answers

How to debug infinite loop in Visual Studio? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges