[Resolved] Ball Still Exhibiting "Boring" Behavior

Hey all, I’m curious if anyone else is having this problem. Even after I use Random.Range() the ball still gets stuck in a loop. It usually happens along the right side, making me think there was something wrong with my right wall. But even after I have verified all the code (and even copy/pasted Ben’s own code from the github for this project) and remaking the wall numerous times, it still gets stuck fairly often. If anybody else has had this problem and has any clue as to what’s causing it, I would definitely appreciate the help. I would post the code (and will if asked), but I think it’s moot since it is now officially the instructor’s code.

I think I know why it occurs and how to solve it. But I am a beginner so let me know if it works.

The code in the video series adds a random amount to the velocity to the right (between 0.0 and 0.2). This means if the ball is on the far left it will move a bit to the right every bounce. Hence the ball doesn’t get stuck there.
Now, the right side is a different story altogether. When the ball gets stuck there,a bit of velocity in the x-direction is added. However the ball is blocked by the wall. The little bit of velocity doesn’t seem enough to prevent the loop from happening, the ball simply hits the wall with too steep an angle to seriously bounce off and instead just graces the wall.

If I am right then:

  1. A collision between your ball and the right end of the field should occur. Test this with a print statement.
  2. It can be solved by either increasing the amount of force added, or perhaps better.

Replace:
Tweak = New Vector2 (Random.Range (0f , 0.2f),Random.Range (0f , 0.2f));

With:
Tweak = New Vector2 (Random.Range (-0.2f, 0.2f),Random.Range (-0.2f , 0.2f));

This way the ball get randomly force added to the right OR the left. Hence it shouldn’t be stuck on one side. Please let me know if this works!

Well, you’re a better beginner than I. Because that worked. Incidentally I had, at one point, jacked the velocity way up (like 1 and 2) just to see if it was working at all. And even that didn’t work to get it off the wall. But this seems to do the trick. Thank you so much.

Glad it worked! You’re welcome!

I put the Random.Range at (-0.5f , 0.5f) for x and (-0.5f , 0f) for y, but when the ball goes side-to side for a while, it still starts to float up. How do I prevent this?

Hi,

You should never make y negative. Gravity is already giving Y a negative pull. So if you add a random negative + gravities constant negative, the ball will start to drift down.

Privacy & Terms