So, a simple bouncing ball; we have a position and a velocity. Each update, we add gravity to the velocity and then apply that velocity to the position to make a sphere move.

If we hit the ground plane (assumed to be at 0 in the Y) then we invert the position and flip the velocity to correct the position and the direction of movement

using UnityEngine;

public class Example01 : MonoBehaviour
{
    public Vector3 m_position;
    public Vector3 m_velocity;
    public Vector3 m_gravity;
    public Transform m_sphere;

    void Start()
    {
        m_gravity = new Vector3(0.0f, -9.8f, 0.0f);
        m_position = new Vector3(0.0f, 0.0f, 0.0f);
        m_velocity = new Vector3(0.0f, 10.0f, 0.0f);
    }

    void Update()
    {
        // Get the delta time
        float delta = Time.deltaTime;

        // Apply gravity to the velocity
        m_velocity += m_gravity * delta;

        // Apply the velocity to the position
        m_position += m_velocity * delta;

        // See if we bounced
        if(m_position.y < 0.0f)
        {
            // If we did then invert the position and the velocity
            m_position.y = -m_position.y;
            m_velocity = -m_velocity;
        }

        // Set the position of the sphere
        m_sphere.position = m_position;
    }
}

In the end, we get this…

Now, you can use the Unity physics system to do the same thing but, personally, I prefer to build all content as it makes the end result completely flexible.

Games don’t necessarily need/have perfect physics. Most games I’ve worked on, you need to do something weird that doesn’t obey standard physics laws, so give yourself the ability to do that by staying in control of how everything works.

Check out our blog posts related to this:
How to get the magnitude of a vector
Using vectors for positions and direction

Keep smiling!

Comment any Questions!


Discover more from

Subscribe to get the latest posts sent to your email.

Leave a comment

Trending