HELP - My Ball slows down

Hello,

I have been puzzling over this problem for a while now and really cannot see my problem. I hope someone can help. As far as I understand my ball should maintain its velocity throughout the level, and once the random.range tweak is inserted should in fact gradually speed up. However my ball slows down before the random.range is put in and after. I also experience ‘boring loops’ even with the random.range.

The random.range change is definetly logging in the console withe ach collision.

I have checked the physics2DSettings (gravity set to -1 in y).

I have played around with the velocity setting and tweak settings and although this has teh expected effect insofar as changes ball direction and initial speed it still always slows down.

This is the script I have for the ball;

void Update () {

	if (!hasStarted) {
	//Lock Ball position to paddle
	this.transform.position = paddle.transform.position + paddleToBallVector;


	//Wait for mouse button to launch
	if (Input.GetMouseButtonDown(0)) {
		print ("Mouse clicked");
			hasStarted = true;

		this.rigidbody2D.velocity = new Vector2 (2f, 20f);
		}
	}
}

void OnCollisionEnter2D (Collision2D collision){ 

	Vector2 tweak = new Vector2 (Random.Range (0f, 0.2f), Random.Range (0f, 0.2f));
	print (tweak);

	rigidbody2D.velocity += tweak;
}

}

Any thought?

Many thanks :slight_smile:

2 Likes

Hello @Ian_Weir,

What is the isKinematic option set to on the Rigidbody2D attached to the ball GameObject?

If it is ticked, untick it and make sure the linear drag and gravity scale are aet to 0.

With regards to the tweak, this alone is not sufficient to completely erradicate the repetitive ricochet issue but increasing the range for your Random.Range will reduce it’s likelihood some more.

1 Like

Thanks for coming back to me Rob. The IsKinematic was untucked. If I set the gravity scale to 0 the same problem occurs, however without gravity the ball eventually stops mid screen :grinning:. I have had a play with the various settings but none seem to alter the deceleration.

When I first made my ball it didn’t behave like the one on the tutorial, which would bounce up to the same height each time. Mine would bounce higher and higher! Which makes this even more puzzling.

I shall go back and make the scene from scratch and observe when the change happens.

Thanks again!

Don’t bother to recreate from scratch the whole scene, you’ll get again the same problem since it’s caused by the physics engine itself, as Rob pointed out.

You can check it by yourself, adding these lines in the ball script, and disabling temporarily the OnCollision method:

void FixedUpdate(){ Debug.Log("Ball velocity: " + ballRigid.velocity.magnitude); }

This will show in the console the magnitude of the velocity vector, and you’ll see that the value will remain always the same (of course), around 20.1 in your case ( sqrt(20^2+2^2) ), even when the ball will start to have a really low y velocity component.

And if you notice, the y component of the velocity retains the same absolute starting value, and it just changes its sign every collision, until the ball hits a brick or the paddle at its corner: at this moment, the physics engine will “transfer” a huge part of the y velocity component to the x component, whereas the magnitude will remain the same.
And the more it happens (hitting corners), the more the x component increases and the y component decreases, up to the point that even adding a random number will have an almost negligible effect.

The solution? Scrap away box colliders from everything, and use polygon colliders with rounded edges, something like this for the paddle, for example:

For the bricks clearly you need to smooth both the upper and lower corners, and I strongly advice to smooth even the cornerns of the side colliders, this way you’ll avoid the y velocity component bug AND will give more control to the player (the more the ball hits the paddle near its edges, the more y component will be transferred to the x component or viceversa, depending on which side of the paddle is hit by the ball).
And you’ll be able to remove that random modifier of the ball velocity, which is really ugly. :smiley:

4 Likes

Hi Galandil,

Apologies for the delay in getting back to u. It’s good to know I’m not going mad and it is the engine! As soon as I get back I’m going to give this a go. Really appreciate you taking the time to help, thank! I’ll let u know how I get on.

Cheers

The solution? Scrap away box colliders from everything, and use polygon colliders with rounded edges,

Brilliant! I was already worried about implementing a random element to the ball’s movement before starting this lecture. It seemed like it would take away control from the player. I was considering a solution where I would calculate the distance between the centre of the paddle and the ball contact-point and change the velocity/direction of the ball accordingly. This is a much more elegant solution…great idea. :slight_smile:

The solution? Scrap away box colliders from everything, and use polygon colliders with rounded edges,

Excuse me for bumping this, but how do you make the edges round though? Can you really make the edges of a Polygon Collider round?

Hey, no problem.

No, in reality you can’t, I meant “diagonal” edges like the ones I showed in the pic.

OR, easier way: use a Capsule Collider 2D, it’s been introduced since the 5.5 version.

how do you do this???

hmm…i just took the box colliders off and put on polygon colliders and my ball flew to the roof and stayed there XD…i must be missing a couple of steps

hey rob have u worked out how to fix this? im reading some stuff here and i dont feel anything is clearly explained…

Hey Blake,

There are a few things going on in this topic, quite an old topic now actually, can you clarify, what is the issue you are having?

  • ball slowing down
  • not sure how to edit the polygon colliders
  • something else

thanks :slight_smile:

1 Like

hey rob…well…im trying to fix my ball slowing down…and the guy above said that was what the issue was…? unless u know another way…?? i find it very hard to understand some of this because its not put in clear context…im having even less luck finding the info i need through the documentation and being able to implement it XD

1 Like

to clarify the guy above said the colliders were the real issue…but im wondering…because its not mentioned in the course so far and that issue dosnt seem present in the videos

1 Like

ok…so i got to the bit of the course that explains polygon colliders and worked out how to replace the wall and roof box colliders with polygons and replaced everything else with polygon including the ball but i still get this slow down effect…also tried getting rid of drag but it dosnt seem to affect it…wondering what the heck i am doing wrong here :confused:

1 Like

ah…ok…so…it looks like i didnt have the physics material created and attached to the ball to decrease friction and add bounciness…

1 Like

Excellent progress and determination :slight_smile:

I hadn’t forgotten you but have been caught up in some plumbing tasks today so have o my been reply via my mobile - not so easy for in depth stuff.

So, with all of your updates to the game, how is the ball responding now? Sounds like a lot of favorable progress :slight_smile:

1 Like

thats ok dude i figured you were busy! XD last i checked the ball seems to be functioning fairly normally but ill need to fully play test it…i actually managed to complete a level with the autoplay function…something that was not possible before because the ball would slow down too much XD woooot!
first you get the video games working…then you get the money…then you get the woman…then you get the power

Privacy & Terms