Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Moving RigidBody
3.
RigidBody.AddForce()
4.
Example
5.
Frequently Asked Questions
5.1.
How does AddForce work in Unity?
5.2.
What is AddForce in Unity?
5.3.
How do you remove all forces from Rigidbody Unity?
5.4.
How do I reset my Rigidbody?
5.5.
What is the ForceMode.Impulse Method?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Moving RigidBody

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In this blog, we will learn about moving RigidBody in Unity using the AddForce method with its example. So, let’s begin with the introduction.

Rigidbodies let your GameObjects interact with physics. The Rigidbody can accept forces and torque to move your items realistically. To be impacted by gravity, operate under extra forces via scripting, or interact with other objects using the NVIDIA PhysX physics engine, each GameObject must possess a Rigidbody.

Source: docs.unity3d.com

 

Rigidbodies must be explicitly added to GameObject before the physics engine may impact them. From the Components->Physics->Rigidbody menu, you may add a Rigidbody to your selected object. Your object is now physics-ready; it will fall under gravity and accept forces, but you may need to add a Collider to get it to behave precisely as you want.

We are done with the introduction to RigidBody. Let’s learn about moving RigidBody now.

Moving RigidBody

There are two methods of moving a gameObject in Unity:

  1. By Changing Position Coordinates: By altering the location of a gameObject without concern for its physics or other similar components. So far, we've done this by simply adding a value to the object's X position per frame.
  2. Rigidbody Physics: When dealing with physics-based objects, it makes more sense to apply forces or adjust their velocity rather than update their location directly. It appears more realistic.
     

In this blog, we'll understand the AddForce() method provided by the class Rigidbody.

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

RigidBody.AddForce()

The Rigidbody's force is increased by using the RigidBody.AddForce() function. Force is applied constantly along the force vector's direction. Changing the ForceMode mode changes the kind of force to Acceleration, Impulse, or Velocity Change.

At the moment of the call, the results of the forces applied using this function are collected. The physics system applies the effects during the following simulation run (after FixedUpdate, or when the script explicitly calls the Physics.Simulate method). 

Because this function has multiple modes, the physics system merely gathers the resulting velocity change rather than the force values passed. Assuming deltaTime (DT) equals the simulation step length (Time.fixedDeltaTime) and mass equal the mass of the Rigidbody to which the force is applied, the velocity change is computed as follows for all modes:

  • ForceMode.Force: Interprets the input as force (measured in Newtons) and changes the velocity by the value of force * DT / mass. The effect depends on the simulation step length and the body's mass.
  • ForceMode.Acceleration: parameter is interpreted as acceleration (measured in meters per second squared), and the velocity is changed by the magnitude of force * DT. The simulation step duration determines the effect but not by the body's mass.
  • ForceMode.Impulse: The parameter is interpreted as an impulse (measured in Newtons per second), and the value of force/mass changes the velocity. The effect is affected by the body's mass but not by the simulation step duration.
  • ForceMode.VelocityChange: The parameter is interpreted as a direct velocity change (measured in meters per second), and the force value changes the velocity. The impact is independent of body mass or simulation step length.
     

Only an active Rigidbody may be subjected to force.AddForce has no impact if a GameObject is inactive. Furthermore, the Rigidbody cannot be kinematic. When a force is applied, the Rigidbody's state is set to awake by default unless the force is Vector3.zero.

Example

The following script will apply a continuous force to our object in the x-direction.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ForceController: MonoBehaviour
{
    Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        rb.AddForce(new Vector3(1f, 0f, 0f), ForceMode.Force);
    }
}

 

We've finished with the blog; let's move to faqs.

Frequently Asked Questions

How does AddForce work in Unity?

Unity describes AddForce as "adding a force to the rigid body." This force is then applied to the GameObject. This is useful for projectile shooting, vehicle launch, pushing away things in an explosion, or moving characters.AddForce, in a nutshell, provides velocity to your GameObjects.

What is AddForce in Unity?

It gives the Rigidbody more force. Force is applied regularly along the force vector's direction. Only an active Rigidbody may be subjected to force.AddForce has no impact if a GameObject is inactive.

How do you remove all forces from Rigidbody Unity?

if you want to remove all forces, use rigidbody.velocity = Vector3. zero 

How do I reset my Rigidbody?

You do that by resetting the velocity and the angular velocity to Vector3(0, 0, 0);.

What is the ForceMode.Impulse Method?

ForceMode.Impulse parameter is interpreted as an impulse (measured in Newtons per second), and the value of force/mass changes the velocity. 

Conclusion

This article extensively discussed the Moving RigidBody and RigidBody.addForce() method and its use.

After reading about Moving RigidBody, are you not feeling excited to read/explore more articles on the topic of Unity? Don't worry; Coding Ninjas has you covered. To learn, see the introduction to game developmentIntroduction to Unity, and Creating animations in Unity.

To learn more about DSA, Competitive ProgrammingSystem Design, JavaScript, etc, refer to our Guided Path on Coding Ninjas Studio. If you wish to put your coding skills to the test, check out the mock test series and enter the contests hosted by Coding Ninjas Studio! However, if you have just begun your learning process and are looking for questions asked by tech giants such as Amazon, Facebook, and Microsoft, among others, you should look at the interview experiences and interview bundle for placement preparations.

Nonetheless, you may want to consider our paid courses to give your career a competitive advantage!

Please vote for the blogs if you find them valuable and exciting!

Happy Learning!

Previous article
Creating animations in Unity
Next article
UNITY 3D CANVAS
Live masterclass