Autoplay loses the game, and ball smashing through the walls

Hi,

I have no idea, but my auto play, although following the ball, it still loses the game. Mainly due to the extreme velocity the ball picks up to and it also causes the ball to hit a wall and disappear into the great void…

Is there a way to limit the max velocity or strength the hit reception of the wall colliders?

Tried using this code, but doesn’t seem to have much effect:

[SerializeField] float maxVelocity = 50f;

   private void MaxVelocity()
    {
      myRigidBody2D.velocity = Vector2.ClampMagnitude(myRigidBody2D.velocity, maxVelocity);
    }
1 Like

I’m also having a problem with autoplay. The paddle hardly moves if at all so the ball just quickly falls into the lose collider right away. I entered all the code in that Rick did unless I just missed something.

This was an excellent adjustment to the code. My second level had a lot more blocks on it and the ball kept speeding up until you almost couldn’t follow it!

I tried your limit of 50 and while that kept it from getting as fast as before it was still pretty fast. I made maxVelocity be 25 in my second try and I think that was much more reasonable as a top speed, at least in my game.

An addition might be to also have a minimum value and then have a random change to it, so it can slow down and speed up slightly on each collision. Maybe make some blocks “sticky” to slow it down and others “bouncy” to speed it up.

I combined it with this and so far my ball never gets stuck in one place: VelocityTweak FIXED

The two together are a nice enhancement to the way the game behaves.

I have made my own version of the Vector2.ClampMagnitude() method. The problem with that method is that it only clamps to a maximum value and I wanted to set both a low and a high value, plus I also wanted to have different blocks either slow or speed up the ball.

I solved this with two methods. The first one you call will either the low or high value you want it to be clamped to. You only call this if it outside the designated range. The second one will change the velocity by a multiple of the current speed, so 0.9f will slow it by 10% and 1.1f will speed it up by 10%, etc.

I have included some Debug.Log() statements that are commented out, in case you want to check what it does in ClampVelocity().

/***
*       ClampVelocity() will set the velocity to the passed value.
***/
private void ClampVelocity(float velocityLimit)
{
	//Debug.Log("Before magnitude = " + myRigidBody2D.velocity.magnitude +
	//	", x = " + myRigidBody2D.velocity.x + ", y = " + myRigidBody2D.velocity.y);
	ChangeVelocity(velocityLimit / myRigidBody2D.velocity.magnitude);
	//Debug.Log("After magnitude = " + myRigidBody2D.velocity.magnitude +
	//	", x = " + myRigidBody2D.velocity.x + ", y = " + myRigidBody2D.velocity.y);
}   // ClampVelocity()

/***
*       ChangeVelocity() will make the ball velocity change by the multiple of the
*   new value given.  The change is not exact and will tend to be a bit over or under
*   the amount asked for, due to rounding, but it will be fairly close.
*		Examples:
*		ChangeVelocity(0.9f) will slow the ball down by about 1/10th
*		ChangeVelocity(1.1f) will speed the ball up by about 1/10th
***/
public void ChangeVelocity(float magnitudeChange)
{
	myRigidBody2D.velocity *= magnitudeChange;
}   // ChangeVelocity()

Enjoy!

Privacy & Terms