 # Boring loops cause

What I think is the true cause for boring loops is when the ball bounces either too horizontaly or to verticaly. The workaround for that should be artificialy adding either the Xspeed or Yspeed to the ball when it bounces in to perpendicular direction while keeping its abs speed constant.

I don’t think a Random.range is a good solution to the problem (it helps, but only a little).

**** looks like a wrong section I would like to second this… although adding the tweak variable to attempt to keep it from getting stuck in a vertical loop, it still does if all the way at right of screen, since the tweak always sends ball slightly right. Even my slanted sides of custom collider will not rectify the situation. It is also annoying that if the only bricks left in the scene are all at left third of frame, the ball keeps on going right right right from center… how can we at least make the ‘tweak’ more random, as in sometimes right & sometimes left? Or as Obolus suggested, perhaps a better solution? Any assistance is greatly appreciated!

Having prior programming experience, I made my own fix to this solution. Still needs a few tweaks, but it does work:
On bounce, check the current velocity. If the Y velocity is within a certain range (I’ve tried -0.4 to 0.4 but that might need to be raised) I accelerate the ball’s velocity in that direction by 0.4 in the direction it was moving. If the X velocity is within the same numeric range, I determine which side of the screen it’s on and change its velocity to move towards the center of the screen in an effort to keep the ball from being trapped against the wall.

As said, this trick has worked, but the numbers still need some tweaking because I think a velocity of 0.5 still can allow for some seriously long boring loops (which seem to happen most in Y Velocity for me).

[details=My Code (Unity 5, should still work with Unity 4)] Vector2 Velocity = this.GetComponent().velocity;
if (Velocity.y < 0.4 && Velocity.y > 0) {
Velocity.y += 0.4f;
Debug.LogWarning (“Avoiding Boring State with += YVel”);
this.GetComponent().velocity += Velocity;
} else if (Velocity.y > -0.4 && Velocity.y <= 0) {
Velocity.y -= 0.4f;
Debug.LogWarning (“Avoiding Boring State with -= YVel”);
this.GetComponent().velocity += Velocity;
}
if (Velocity.x > -0.4 && Velocity.x < 0.3) {
float ballPos = this.transform.position.x;
if (ballPos <= 8) {
Velocity.x += 0.4f;
this.GetComponent ().velocity += Velocity;
} else {
Velocity.x -= 0.4f;
this.GetComponent ().velocity += Velocity;
}
}[/details]

One thing I really think would help with this problem in my code, in particular, is for the Vertical Boring Loop is actually modifying the velocity to the Absolute Value of its movement +/- the 0.4 increase. This would require using some Math functions not covered in the course, though.

1 Like

try this, its simple and I used addTorque to spin my ball

public void AddForce(Vector2 force, ForceMode2D mode = ForceMode2D.Force);

I found ForceMode2d mode of “Impulse” to work well.

1 Like

btw had some problems (forgot to specify ‘new’ but here is some code that worked

1 Like