Answer the question
In order to leave comments, you need to log in
How to properly set up ball physics in Unity?
Unity has this scene in 3D: A
pink ball moves at a constant speed, ricochets when it hits a wall and obstacles.
Here are the settings of the RigidBody component of the ball
Box Collider hangs on walls and obstacles, there is no RigidBody.
Below I attach the code that hangs on the ball and is responsible for the movement of the ball and the ricochet.
public float startSpeed;
public float increasedStep;
public float maxSpeed;
private void FixedUpdate()
{
rb.velocity = direction * startSpeed;
}
private void OnCollisionEnter(Collision other)
{
ContactPoint contactPoint = other.contacts[0];
direction = Vector3.Reflect(direction, contactPoint.normal);
if(startSpeed + increasedStep > maxSpeed) return;
startSpeed += increasedStep;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question