# I need help with a ball in brick breaker

Updated on March 21, 2017 in
7 on February 23, 2017

I have been struggling with this problem for a week and I stopped creating the game for 3 days then returned to it hoping that I will know something . My problem is when there is something related to Random.Range . it always make troubles and doesnt work efficiently with me. the problem is when I launch the ball and crashes to one of the bricks it should go randomly to another destination. However, it get slows with every crash with the bricks and it doesnt go to the right. I did not understand anything about Random,Range and the numbers . I do know that the velocity is the  speed but nothing more .may be cz I learnt math in my language that is way I find it difficult . I always use experiments when I dont understand a code so that I can teach my self and that way is far more effective than just learning from a tutor
http://pastebin.com/X9Uev55E
this my code

• Liked by
on February 24, 2017

Do you have a RigidBody on your ball? How does it know to ‘bounce’ away from the object it’s colliding with? It looks like your adding an upward launch velocity with another upward collision velocity  which only changes the x value a small amount compared to the initial x velocity…

• Liked by
on February 24, 2017

The Ball has rigidbody and collider and physics material All things are ready the only thing is wrong are the figures in the script which controls the ball where to bounce and why does it slows down everytime it collides with a brick .

• Liked by
on February 25, 2017

Hmm… I’m having a hard time picturing exactly what’s happening… Maybe post a vid or gif of what you see?

• Liked by
on February 25, 2017

I hope you  dont mind the voice and the annoying sound. This is my second time recording ty
https://youtu.be/48wiVluHloI

• Liked by
on February 28, 2017

anyone to help ? I have a problem with this ball speed. I want to to be stable but i dont know how .

• Liked by
on March 21, 2017

I’ve seen this problem before when people try to make Breakout games and it comes from not understanding how vectors work.  It’s not an insult in any way — but if you go and brush up on how vectors and velocity work, it won’t take nearly as much time and all your programming will be better for it.

One of the most common misconceptions is that the random vector you get when you bounce off something is your current velocity.  It’s not.  Your current ball velocity after a bounce is based on four things: your previous speed, your previous direction, your new speed, and your new direction.  All of that added together is basically your velocity.

So often what’s happening when you try to add a randomized direction after a bounce is that you’re not taking into account your previous bounce velocity.  ,:  You when you add a random direction and speed, you’re either increasing your current velocity or decreasing.  Sometimes you might decrease it so much, it increases in the other direction, but your overall speed will still appear decreased.

Picture a game of musical chairs.  You start out on the left-most chair when the music starts.  We’ll cal this chair #1.  As you go five chairs over (chair #6), the music stops.  You scramble to any random chair that’s available within reach: your current chair or one up to two chairs away in either direction.  So you end up in chair #4, #5, #6, #7, or #8.

Now let’s say an outside observer comes in to restart the music and he notices how far you’ve moved.  Now if you moved from Chair #1 to Chair #8, it must seem like you moved pretty quick when he wasn’t looking.  But if you only moved from Chair #1 to Chair #4, he might wonder what you were doing the whole time.

You’re looking at the ball kind of like this outside observer.  It’s got two velocity to take into account — it’s previous velocity that it was traveling and its new velocity.  But you don’t see these little calculations.  You only see where the ball starts from to where it ends up.  From your perspective, it’s speeding up or slowing down randomly.  What’s actually happening is that it’s calculating its new position based on two different velocity vectors that both get taken into account.

If you look up some youtube videos in basic vector math that can explain it with diagrams that make it far easier to understand than just reading about it.  It’s easier to grasp visually then to read about it.  It’s hard to describe, but when you see how it’s easier to show with a couple of line drawings as you show the directions of the object.

You just have to understand that your “fly” isn’t changing the velocity — you’re adding or subtracting to it.  And neither of these two velocities are your apparent speed.  Apparent speed is based on the sum of these velocities (after they’ve been added together).  That’s why you may not be getting the results you want.

• Liked by
on March 21, 2017

I don’t think this is a programming issue so much as a problem with the vector math.  I think it might help to look at some Youtube videos to brush up on the basics of how vectors and velocity work, especially when added or subtracted after a collision.

One of the most common misconceptions is that the random vector you get when you bounce off something is your current velocity.  It’s not.  Your current ball velocity after a bounce is based on four things: your previous speed, your previous direction, your new speed, and your new direction.  All of that added together is basically your velocity.

So often what’s happening when you try to add a randomized direction after a bounce is that you’re not taking into account your previous bounce velocity.  ,:  You when you add a random direction and speed, you’re either increasing your current velocity or decreasing.  Sometimes you might decrease it so much, it increases in the other direction, but your overall speed will still appear decreased.

Picture a game of musical chairs.  You start out on the left-most chair when the music starts.  We’ll cal this chair #1<a href=”http://forum.brackeys.com/hashtag/1/” target=”_blank” rel=”nofollow”>.  As you go five chairs over (chair #6), the music stops.  You scramble to any random chair that’s available within reach: your current chair or one up to two chairs away in either direction.  So you end up in chair #4, #5, #6, #7, or #8.

Now let’s say an outside observer comes in to restart the music and he notices how far you’ve moved.  Now if you moved from Chair #1 to Chair #8, it must seem like you moved pretty quick when he wasn’t looking.  But if you only moved from Chair #1 to Chair #4, he might wonder what you were doing the whole time.

You’re looking at the ball kind of like this outside observer.  It’s got two velocity to take into account — it’s previous velocity that it was traveling and its new velocity.  But you don’t see these little calculations.  You only see where the ball starts from to where it ends up.  From your perspective, it’s speeding up or slowing down randomly.  What’s actually happening is that it’s calculating its new position based on two different velocity vectors that both get taken into account.

If you look up some youtube videos in basic vector math that can explain it with diagrams that make it far easier to understand than just reading about it.  It’s easier to grasp visually then to read about it.  It’s hard to describe, but when you see how it’s easier to show with a couple of line drawings as you show the directions of the object.

You just have to understand that your “fly” isn’t changing the velocity — you’re adding or subtracting to it.  And neither of these two velocities are your apparent speed.  Apparent speed is based on the sum of these velocities (after they’ve been added together).  That’s why you may not be getting the results you want.

• Liked by