Beginners guide to Unity Physics 3D

Simulate realistic 3D physics in Unity with Nvidia PhysX engine

·

5 min read

There are a lot of things that go into making a game realistic, physics is one of the main reasons for it. With physics, we can apply different kinds of force on an object, like collisions, gravity, friction, drag and so on.

Well, you have two options for using physics,

Types of Physics Engine

  1. Built-in 3d Physics Engine:

    An open-source physics engine developed by NVIDIA known as the PhysX engine, is integrated within the Unity Editor. It is suitable for most use cases such as first-person and third-person games, racing games, and platformers.

  2. Unity Physics Package:

    This is a new physics engine that is developed with the collaboration between Unity and Havok as a part of Unity’s Data-Oriented Technology Stack (aka DOTS framework). It features higher performance, multi-threaded executions, scalability and optimized network multiplayer games. It is available via Package Manager for Unity 2022.2 and above.

Here are some basic differences between the two,

FeaturesBuilt-in 3d Physics EngineUnity Physics Package
Physics engineNvidia PhysX engineHavok physics engine
Project typeObject-oriented 3D projectsData-oriented 3D projects
InstallationAvailable out-of-the-box in Unity EditorFrom the Package Manager
Features and componentsRigidbody, Collider, Joint, Articulation, Cloth, Ragdoll physicsUnity Physics, Havok Physics for Unity (extension)
LicensingFree to useHavok Physics for Unity is subject to a specific licensing scheme

In this blog post, we will be exploring Built-in 3d physics engine, which is the most common Physics engine that Unity offers, also because of its beginner friendliness.

Built-in Physics 3D topics covered:

Rigidbodies

Rigidbodies enable objects as though they have different components of physics such as mass, gravity, ability to collide with other objects, friction, velocity, momentum and inertia. They are also used by other physics components that we will be exploring later, like joints and character control. The above GIF shows gravity being added to a GameObject.

To use this, select the GameObject in the hierarchy panel, in the inspector panel and add Rigidbody component.

Colliders

They are essential for collisions to occur among the GameObjects. Think of it as an invisible shape that is on your object, all the physics works on the basis on this invisible shape and NOT the object that you see. For instance, if you were to create a cube and attach only sphere collider to it. It behaves like a sphere even though it looks like a cube. As shown below, (I have added rigidbody component to both the GameObjects to enable gravity)

In order for the collision to occur, both must have colliders component attached (Plane and Cube in this case). Although they are invisible, they make the interaction more realistic. There are a few out-of-the-box colliders offered by Unity (aka Primitive Colliders) like sphere, box, capsule etc. It is recommended to use Primitive Colliders for optimal performance.

To use this, select the GameObject, in the inspector and add the appropriate collider component.

Triggers

These are mainly used to detect collision, for example when a player reaches this point, play this animation. You might have noticed some cutscenes getting triggered when you reach a certain point in a game. Well, now you know how.

To use this, add the appropriate collider component and check the is trigger box. Do note that the collision won't work if it is set to is trigger.

Character Control

This is how you give the character the ability to move around in the scene. A Character Control is a capsule-shaped collider component with specialized features for behaving as a character in a game, such as gravity, slope limit, step offset, and skin width.

It can be configured with a C# script to some add features such as basic movement physics, turning with torque, and jumping with force.

To use this, add Character Controller component and attach the below custom script to the GameObject.

private CharacterController characterController;
    public float Speed = 6f;

    // Start is called before the first frame update
    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        characterController.Move(movement * Time.deltaTime * Speed);
    }

Joints

Joints can be used to create realistic and complex physics simulations, such as ragdolls, doors, chains, pendulums, and wrecking balls. There are various types of Joints, the above shown is an example for Spring Joint. I am moving the white plank on top, and the attached black cube follows as shown.

To use this, you must first attach rigidbody to both the GameObjects, add appropriate joint components to the object that you wish to see the joint behavior (black cube) and finally add the rigidbody of the reference object (white plank).

Conclusion

In this blog post, we explored the basics of unity physics that helps in creating realistic and interactive simulations. By using this, you can create amazing physics-based games and applications that will engage and delight your users. You can also experiment with different settings and scenarios to discover new possibilities and challenges.

If you found this blog post helpful, please share it with your friends and colleagues who are interested in unity. You can also leave a comment below and let me know what you think or if you have any questions.

That's all for now. Thank you for reading and happy coding!