Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Mar 27, 2024

Physics in Unity

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

Introduction

The built-in Physics Engine in Unity handles physics. Unity's built-in Physics Engine runs the physics for game object interactions and various effects such as gravity, acceleration, collisions, etc. In this blog we will learn about some of the key concepts of built-in 3D physics engine in Unity.

Rigidbody 

Rigidbody Allows a GameObject to have physical behavior. It will interact with other GameObjects with Colliders if one is attached to it. A Rigidbody also has a scripting API that allows you to apply forces and control an object in a physically realistic manner.

The Unity Physics Engine only knows that an object is meant to be acted on by physics if the Rigidbody component is added to the game object; otherwise, the engine considers the game object static and immobile. When a stationary game object (one that does not have a Rigidbody) moves, the Physics Engine re-runs the calculations to optimize the static objects in the scene. 

To ensure that Unity runs as smoothly as possible, use a Rigidbody on any game object that will move. At least one of the two objects interacting during an OnCollision or OnTrigger event must also have a Rigidbody. Many properties of the Rigidbody component can be changed to make the game object react to their forces and collisions differently.

 

Rigidbody

Source

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Mass

The Physics Engine measures the mass of the Rigidbody on the game object in kilograms. Like in real life, a game object with a higher mass reacts more strongly than one with a lower mass.

Representation of mass

Source

Collider 

Collider Defines a GameObject's shape for physical collisions. Colliders, which attach to GameObjects and define the shape of a GameObject for physical collisions, are used by Unity to handle collisions between GameObjects. A collider is undetectable and does not have to be the same shape as the GameObject's mesh.

A rough mesh approximation is often more efficient and indistinguishable in gameplay.

 

Collider object

Source


Primitive colliders are the simplest (and least processor-intensive) collider types. These are the Box Colliders in 3D. Sphere Collider, for example, as well as Capsule Collider. You can use the Circle Collider 2D and Box Collider 2D in 2D. If you want to make compound colliders then you can combine any number of these into a single GameObject.

Material

If you want the Collider to have a unique Physics Material, use Material. The friction and bounciness of the Collider can be adjusted using a Physics Material.

Joints

A joint joins one rigidbody to another or a fixed point in space. Joint forces move rigid bodies, and joint limits limit that movement. Rigidbodies have the following degrees of freedom thanks to joints.

 

Joints

Source


The following Unity joint components apply distinct forces and restrictions to rigidbody components, as well as provide those bodies varying motion:

Character Joint

A ball and socket joint, such as a hip or shoulder, is imitated. All linear degrees of freedom are constrained, whereas all angular freedoms are enabled. Rigidbodies connected to a Character Joint orient themselves around each axis and pivot from a common origin.

Configurable Joint

Any skeleton joint, such as those seen in a ragdoll, is emulated. This joint can be configured to force or restrict rigid body movement in any degree of freedom.

Fixed Joint

Allows a stiff body to follow the movement of the rigid body to which it is attached. When you need rigid bodies that can quickly break apart from each other, or when you want to connect the movement of two rigid bodies without using a Transform hierarchy, this is handy.

Hinge Joint

Attaches a rigid body to another rigid body or a point in space at a common origin, allowing the rigid bodies to spin around a common axis. It can be used to simulate doors and finger joints.

Spring Joint

Separates solid bodies while allowing the gap between them to stretch slightly. The spring works like an elastic band, pulling the two anchor points together into the same position.

Physical Articulations

A physics articulation is a logical tree of Articulation Bodies in which each parent-child relationship represents mutually constrained relative motion.

The primary goal of physics articulations is to provide realistic physics behavior for non-gaming commercial and industrial applications that involve joints. For example, they make it much easier to simulate robotic arms and kinematic chains than regular Joints.

Character controller

A character in a first- or third-person game usually requires collision-based physics to avoid falling through the floor or walking through walls. Many applications intentionally make the character's acceleration and movement less physically realistic, allowing the character to accelerate, brake, and change direction almost instantly and without being affected by momentum.

 

Character Controllers

Source

This type of behavior can be created in 3D physics using a Character Controller. This component provides the character with a straightforward, capsule-shaped collider that is always upright. The controller has its unique functions for determining the speed and direction of the object, but unlike true colliders, no rigidbody is required, and momentum effects are not realistic.

Physics in multiple scenes

Multiple physics Scenes can be used to manage or work around complex physics contexts. You can, for example, create and configure independent Scenes with physics properties that differ from those of the main scene.

  • To predict GameObject collisions and trajectories, you can instantiate multiple physics Scenes based on the main scene. 
     
  • You can isolate a highly detailed character in its physics Scene to easily filter collisions with other physics scene elements.
     
  • To improve determinism in your physics environment, you can create pre-populated physics Scenes that you can destroy and reload.

Physics Debugging

The Physics Debug Visualiser lets you quickly inspect Collider geometry in your scene and profile common physics-based scenarios. It illustrates which GameObjects should and should not collide with each other. This is especially useful if your scene contains many Colliders or if the Render and Collision Meshes are out of sync.

Physics Debug can profile and troubleshoot physics activity in your game. To assist in locating the source of action, you can customize which types of Colliders or Rigidbody components appear in the visualizer.

Frequently Asked Questions

What is the default mode of collision detection?

Discrete is the default mode of Collision Detection. It is the least resource-intensive collision detection method and is sufficient in most cases. You should only deviate from this when you encounter collisions with fast-moving game objects.

What are interpolate and extrapolate?

Interpolate: The least resource-intensive option uses previous frames to smooth out movement.

Extrapolate: The least resource-intensive option uses the next frame to smooth out movement and consumes many resources.

What is angular drag?

Angular Drag functions similarly to Drag but only applies resistance to the rotational speed of the game object, which in most cases behaves similarly to drag. The exception is when the game object moves without rotating, such as when falling straight down.

Are the physics of Unity deterministic?

Havok Physics for Unity is a stateful but deterministic engine. This means that unless all internal simulation caches are copied, a copy of a physics world will not simulate identical to the original world.

Conclusion

In this article, we have discussed concepts of Physics in Unity. We started with an introduction and moved forward with various key contents like rigidbody, mass, collider, material, joints, physical articulations, character controller, and physics in multiple scenes, then concluded with physics debugging.

We hope that this blog has helped you enhance your knowledge regarding Physics in unity and if you would like to learn more, check out our other articles on our platform Coding Ninjas Studio.

For peeps out there who want to learn more about Data Structures, Algorithms, Power programming, JavaScript, or any other upskilling, please refer to guided paths on Coding Ninjas Studio. Enroll in our courses, go for mock tests and solve problems available and interview puzzles. Also, you can put your attention towards interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help other ninjas grow.

Do upvote our blog to help other ninjas grow.

Happy Coding!

 

Topics covered
1.
Introduction
2.
Rigidbody 
3.
Mass
4.
Collider 
5.
Material
6.
Joints
6.1.
Character Joint
6.2.
Configurable Joint
6.3.
Fixed Joint
6.4.
Hinge Joint
6.5.
Spring Joint
7.
Physical Articulations
8.
Character controller
9.
Physics in multiple scenes
10.
Physics Debugging
11.
Frequently Asked Questions
11.1.
What is the default mode of collision detection?
11.2.
What are interpolate and extrapolate?
11.3.
What is angular drag?
11.4.
Are the physics of Unity deterministic?
12.
Conclusion