-
-
Notifications
You must be signed in to change notification settings - Fork 27
Home
- Is the source project free to use?
- The ball gradually slows down over time
- The ball slows down after colliding with something
Yes, you are free to use the project for any purpose. However, keep in mind that copyright and/or trademark laws can still apply to the original material since many of the games in my tutorials were not originally authored by me. I open source my own code for the community to learn from, but the project is intended for educational purposes only.
Any physics object with drag will naturally slow down over time, even if it does not collide with anything. Make sure the ball's Rigidbody2D
component has the "Linear Drag" and "Angular Drag" set to 0. This will prevent it from slowing down due to drag. If this still does not solve your problem, see the next question below.
Make sure you have added a PhysicsMaterial2D
to the ball's rigidbody with the Friction
set to 0 and the Bounciness
set to 1. However, this doesn't fully fix the problem. If the ball hits an object in a weird way, usually in the corners, then it can still slow down due to weird physics interactions. Ideally this shouldn't happen with those settings on the physics material, but sometimes we are at the mercy of the physics simulation, and it does not always work exactly how we want.
That said, there is still a solution. We just need to add a little bit of code to our Ball.cs
script.
private void FixedUpdate()
{
rigidbody.velocity = rigidbody.velocity.normalized * speed;
}
This code will ensure the ball's velocity remains constant the entire time. You will probably need to decrease the value of the speed
property, though. In the video, we used a value of 500, but with this new code a value of around 10 works pretty well. Feel free to adjust it to your preference.