x


Ball Bounce with Random Range

Following a tutorial, I have created a ball that bounces along 3 walls with a paddle at the bottom, like the game Pong. Sometimes the ball gets stuck in a straight line bounce loop, bouncing from one wall to the other and back with no variation. I'd like to add a bit of randomness to the bounce angle to the ball so that it doesn't get stuck.

I found a similar question answered already, but it was answered with C#, and the tutorial script is in javascript. Everything I tried did not work. Would Random range and vector help? I don't know how to implement them into the script.

Part of the script added to the ball:

function Awake(){
 rigidbody.AddForce(8, 4, 0, ForceMode.Impulse);
 InvokeRepeating("IncreaseBallVelocity", 2, 2);
}

function IncreaseBallVelocity(){
 rigidbody.velocity *= 1.05;
}
more ▼

asked Jul 15 '12 at 12:32 AM

wakeupscreaming gravatar image

wakeupscreaming
0 1 1 2

Can anyone just translate the C# Answers from "Breakout Pong Ball Stuck to Wall Problem" to Javascript? Please help me. http://forum.unity3d.com/threads/138770-Breakout-Pong-ball-stuck-to-wall-problem

It's just a direction of vector problem.

Jul 17 '12 at 04:19 AM wakeupscreaming
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I guess you could just add some force when it hits:

 function OnCollisionEnter(other : Collision) {
      rigidbody.AddForce(Random.insideUnitCircle * 10, ForceMode.Impulse); // or some other multiplier
 }
more ▼

answered Jul 15 '12 at 12:41 AM

whydoidoit gravatar image

whydoidoit
33.1k 11 23 100

I tried this and I thought it might have worked. But today it got into another loop. The ball started bouncing from one side of the wall, to the other, and back and forth. I was hoping a little random variation in it's bounce angle would get it out of those loops?

Maybe the title of my question is wrong. I'm really looking for some random bounce in the ball, specifically in the angles, when it hits a surface. So if the ball bounces onto a surface at 90 degrees, maybe the bounce off would be 88, or 91 degrees (just a random degree or 2 off) the other way.

Jul 17 '12 at 02:00 AM wakeupscreaming

Try a bigger force?

Jul 17 '12 at 02:57 AM whydoidoit

I used a higher force. What I'm noticing is that the ball then has a higher propulsion -- it goes faster, or slower. I'm not really looking for something that goes faster, or has anything to do with the speed of the ball at all.

I want the angle of the bounce to be random. Here is a similar question, not the exact problem, but similar: http://forum.unity3d.com/threads/138770-Breakout-Pong-ball-stuck-to-wall-problem

All the answers are in C#. I'm working in Javascript.

Thanks.

Jul 17 '12 at 03:12 AM wakeupscreaming

Right so take the velocity magnitude before you apply the force and map it back after:

   var speed = rigidbody.velocity.magnitude;
   rigidbody.AddForce(Random.insideUnitCircle * 1000, ForceMode.Impulse); // or some other multiplier
   rigidbody.velocity = rigidbody.velocity.normalized * speed;
Jul 17 '12 at 03:15 AM whydoidoit

Just FYI, with a 1000, it still got into a vicious loop bounce cycle from wall to wall. What does the 1000 mean? So if 1000 still gets the ball bouncing from one wall to the other, then should I put in 10000?

Jul 17 '12 at 03:55 AM wakeupscreaming
(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:

x3460
x572
x169
x117

asked: Jul 15 '12 at 12:32 AM

Seen: 845 times

Last Updated: Jul 17 '12 at 10:53 AM