Ball Bouncyness

Hello all! Nice to meet you. Really loving the course so far, put it away for a little while but now I’m back.
For context I’m an artist in the industry by trade but I wanted to learn to code to make my own games.

At the moment I’m doing the 2D course, working on the block breaker project. I always like adding my own twist to things so I’m making the block breaker all about marbles. It’s all fun so far, my only problem is that once the ball hits a surface I do want it to spin, so I need friction (or a way to add that rotation back without friction). The balls have custom designs and it’s nice to see them spin, they also have a shine overlay that doesn’t spin making them look more realistic (or at least more believable). Here’s my ball and paddle example so it would be easier to visualise (the background is still a sketch please ignore that haha):

Once I add the friction the ball does start slowing down, of course. I thought I’d work around this by also adding a 2d physics material to the paddle with 0 friction but 1.1 bouncyness, so only when the ball hits the battle the speed would increase, but decrease everywhere else. This kind of works unless the ball hits the paddle too many times in a row in which case it starts going too fast and breaks physics.

Would anyone know of a way to add a speed limit to the ball? Or perhaps add the ball rotation back in without the friction?

1 Like

Hi Squibs,

Welcome to our community! :slight_smile:

Would anyone know of a way to add a speed limit to the ball?

Vector maths could help. Normalise the velocity and multiply it by your desired speed. You don’t have to do the maths yourself. Just check the Vector3 struct in the API.

Or perhaps add the ball rotation back in without the friction?

Just an idea, which I didn’t test: Instead of physics materials, you could call AddTorque on the Rigidbody2D component whenever your ball/ marble collides with another collider. Alternatively, you could try to modify the angularVelocity.

If you don’t get a perfect result at the first attempt, don’t hesistate to test different modes (ForceMode2D, see the API) and values. In most cases, if the result looks somewhat right, the rest is rather about finetuning things.

If you have a rough idea, it’s a good idea to take a look at the API because, in most cases, you won’t have to reinvent the wheel. For instance, if your idea is about moving or rotating game objects, check the Rigidbody2D class first. It might well be that you will find a public method or property that sounds useful.

Hopefully, this helped. :slight_smile:


See also:

2 Likes

Thanks so much! This helps a ton, there’s a lot I hadn’t considered.
And thank you for letting me know there’s already tons of resources I can access, I always forget about the API.

Just have to mess around with it until it’s fun to play now!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms