Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why Rigidbody?
3.
Rigidbody component reference
3.1.
Parenting
3.2.
Scripting
3.3.
Animation
3.4.
Colliders
4.
Collision Detection in Real-Time
5.
The Character Controller
6.
Frequently Asked Questions
6.1.
Is a collider required for a rigidbody?
6.2.
What exactly is a drag rigidbody?
6.3.
What are angular and linear drag?
6.4.
How are collisions detected?
7.
Conclusion
Last Updated: Mar 27, 2024

Rigidbody in Unity.

Author Prerna Tiwari
0 upvote

Introduction

In this blog, we will learn about Rigidbody which is an essential part of Unity. Rigid bodies allow your GameObjects to interact with physics. The Rigidbody can accept forces and torque to move your objects realistically. These rigid bodies can be influenced by gravity, or interact with other entities via the NVIDIA PhysX physics engine.

 

 source

Why Rigidbody?

The use of forces is the most significant distinction between manipulating the Transform and the Rigidbody. Transforms cannot receive forces or torque, but rigidbodies can. It is possible to translate and rotate transforms, but this is not the same as using physics. When you try it for yourself, you'll notice a significant difference. Adding forces/torque to the Rigidbody changes the position and rotation of the Transform component. This is why you should only use one of them. Changing the Transform while using physics may cause collisions and other calculations to fail.

Rigidbodies must be explicitly added to  GameObject before the physics engine can affect them. From the Components->Physics->Rigidbody menu, you can add a Rigidbody to your selected object.

Your object is now physics-ready; it will fall under gravity and accept forces via scripting, but you need to add a Collider or a Joint to get it to behave exactly as you wanted.

Rigidbody component reference

Following are the component references of Rigidbody:

Parenting

When under physics control, an object moves in a semi-independent way of how its transform parents move. If you move any parents, they will drag the Rigidbody child with them. Rigidbodies, on the other hand, will still fall due to gravity and react to collision events.

Scripting

Scripts will be used primarily to add forces or torque to your Rigidbodies. This is accomplished by invoking AddForce() and AddTorque() on the object's Rigidbody. When using physics, keep in mind that you should not directly alter the object's Transform.

Animation

In some cases, such as when creating ragdoll effects, switching control of the object between animations and physics is required. Rigidbodies can be marked isKinematic for this purpose. While the Rigidbody is marked isKinematic, it is unaffected by collisions, forces, or any other aspect of the physics system. You'll have to manipulate the Transform component directly to control the object. Kinematic Rigidbodies affect other objects but are not affected by physics. Joints attached to Kinematic objects, for example, will limit other Rigidbodies attached to them, and Kinematic Rigidbodies will affect other Rigidbodies through collisions.

Colliders

Colliders are a type of component that must be added to the Rigidbody for collisions to occur. If two Rigidbodies collide, the physics engine will not calculate a collision unless both objects are also equipped with a Collider. Rigidbodies with no colliders will pass through each other during physics simulation.

                                                                                                                  source

Collision Detection in Real-Time

Continuous collision detection is a feature that prevents colliders from passing each other at high speeds. Normal (Discrete) collision detection can occur when an object is on one side of a collider in one frame and has already passed the collider in the next frame. To address this, enable continuous collision detection on the fast-moving object's rigidbody. Set the collision detection mode to Continuous, and this prevents the rigidbody from passing through any static(i.e.,non-rigidbody) MeshColliders. Please set it to Continuous Dynamic to prevent the rigidbody from passing through any other supported rigidbodies with the collision detection mode set to Continuous or Continuous Dynamic. Box-, Sphere-, and CapsuleColliders all support continuous collision detection.

 

The Continuous Dynamic property of a thin stick GameObject is enabled. The stick does not make contact with the sphere when rotating quickly around the pivot point.

source

 

Continuous collision detection is a safety net to catch collisions in cases where objects would otherwise pass through each other. Still, it will not deliver physically accurate collision results, so if you run into problems with fast-moving objects, you should still consider decreasing the fixed Timestep value in the TimeManager inspector to make the simulation more precise.

A thin stick GameObject that has the Speculative CCD turned on. When the stick rotates quickly around the pivot point, it comes into contact with the sphere, resulting in a collision.

source

The Character Controller

The Character Controller is a player component that you can add. Its purpose is to move the player based on the environment (the colliders). It neither responds nor employs physics in any way.

The Character Controller's core concept is that it provides basic collider responses without physics. Essentially, you will move your player as you would with a Transform, but you will be unable to pass through colliders.

The primary benefit of using this technique is the level of control we'll have over how your player behaves, but the disadvantage is that you'll have to code almost everything.

Rigidbody has a lot more functions for interacting with physics than the Character Controller, so we'd have to code more for the same feature with the Character Controller.

Frequently Asked Questions

Is a collider required for a rigidbody?

The colliders use physics hence, rigidbodies are required on GameObjects in Unity3D. Because static GameObjects do not use physics, you do not need rigidbodies, but you must include at least one in the calculation.

What exactly is a drag rigidbody?

Drag can be used to slow an object down—the greater the drag, the slower the object.

What are angular and linear drag?

Drag is the propensity of any object to slow down due to friction with the surrounding air or water. The linear drag affects positional movement and is configured independently of the angular drag, which affects rotational movement.

How are collisions detected?

A collision occurs when both the horizontal and vertical edges overlap. We examine whether the right side of the first object is greater than the left side of the second object and whether the right side of the second object is greater than the left side of the first object, similarly to the vertical axis.

Conclusion

In this article, we have discussed the concept of rigidbody in Unity. We started with an introduction to rigidbody and why we prefer rigibody, then moved further with parenting, scripting, and collision detection in real-time, and in the end concluded with the character controller.

We hope that this blog has helped you enhance your knowledge regarding rigidbody 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. 

Happy Coding!

Live masterclass