- Home /
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;
}
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.
Answer by whydoidoit · Jul 15, 2012 at 12:41 AM
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
}
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?
$$anonymous$$aybe 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.
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.
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, Force$$anonymous$$ode.Impulse); // or some other multiplier
rigidbody.velocity = rigidbody.velocity.normalized * speed;
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?
Your answer
Follow this Question
Related Questions
Ping Pong Reflect 2 Answers
How do i make a Character/object bounce using Javascript? 2 Answers
Script not working (Java script) 2 Answers
Code to randomly generate a mesh? 0 Answers
Random spawn timer 1 Answer