x


Bouncing Ball and Walls

I am currently making a version of Pong with a little twist. Everything is going great so far, but if the ball is hit at a vertical/near vertical angle, it will continue to bounce between the two walls and pretty much stays there for the rest of the game. I have primarily made the game using built-in Physics materials, although the paddles and the walls are adding force to the ball when it collides with them(a feature of the game, to make it go faster). The ball has force applied to it at the start. I was wondering whether there would be anyway to prevent this "perfect bounce" scenario, and would be very grateful for any help.

Here are what the walls are doing (the opposite force for the bottom wall):

function OnCollisionEnter (collision : Collision) {
if (collision.gameObject == ball) {
       audio.PlayOneShot(WallBoink);

       collision.rigidbody.AddForce (0,-20,0);
    }
}   

And here are what the paddles are doing (opposite force for opposing paddle):

function OnCollisionEnter (collision : Collision) {
if (collision.gameObject == ball) {
       audio.PlayOneShot(Boink);

       collision.rigidbody.AddForce (-20,0,0);
    }
}   

Any help would be greatly appreciated.

more ▼

asked May 19 '11 at 06:11 AM

Jahannam gravatar image

Jahannam
16 1 1 2

How did you make the ball move that way?

Jan 08 at 07:42 PM Tijs
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

How about making some sort of bounce counter, which is reset after it hits a paddle, and if it bounces against the wall n times, then a force is added which goes sideways? Sorry if I didn't explain that too well, if you need me to write out the code I will.

more ▼

answered May 19 '11 at 09:11 AM

Muzz5 gravatar image

Muzz5
1.2k 65 83 94

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1879
x117
x35

asked: May 19 '11 at 06:11 AM

Seen: 2299 times

Last Updated: Feb 07 at 12:14 PM