Applying Forces to Objects

Simon Pham
3 min readMay 3, 2024

--

Why do we need to apply forces to objects in game development?

Forces are a fundamental concept used in game development to create realistic and interactive experiences. They can be applied to GameObjects to simulate various physical phenomena such as gravity, friction, and acceleration. For example, in a shooting game, forces can be applied to projectiles like bullets or arrows. Also, explosions are often simulated by applying forces radially outward from the explosion center.

Demonstration

Scene setup

To demonstrate how forces work in Unity, I’ve set up a scene with a portal and a container.

Our objective is to apply forces to the container to launch it through the portal.

Applying forces to the container

First, let’s ensure that both GameObjects have a collider and the container has a Rigidbody component with Collision Detection set as Continuous Dynamic. Next, I’m going to create a script called ApplyForces and attach it to the container.

There are two types of force that we can apply to this container: a regular force and a rotational force called torque. To understand the difference between the two, you can imagine that you’re trying to open a nut with a wrench. When you do that, you’re applying a force which creates a rotational motion in the nut called torque.

In Unity, we can use the AddRelativeForce and AddRelativeTorque methods to add a force and a torque to an object, respectively. We will use AddRelativeForce to launch the object upward and forward, and use AddRelativeTorque to rotate the object so that it faces where we want it to.

First, we need to get a reference to the Rigidbody and create a variable for a force and a torque value.

private Rigidbody _rb;
[SerializeField] private float _forceValue;
[SerializeField] private float _torqueValue;

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

In the Inspector, I’m gonna set the value for the force and torque to be 7 and 4, respectively.

Both AddRelativeForce and AddRelativeTorque methods accept a Vector3 and a ForceMode as their parameters. The Vector3 represents the direction you want to apply the force. For the ForceMode, Unity offers four different properties including Force, Acceleration, Impulse, and VelocityChange.

Since I want to take the container’s mass into account, I will only care about Force and Impulse. For the AddRelativeForce method, I want to launch the container in an arc trajectory, so an initial push would be enough, and to do that, I’m going to go with the Impulse property. For the AddRelativeTorque method, I want the object to continuously face a specific angle, so I’m going to go with the Force property.

using UnityEngine;

public class ApplyForces : MonoBehaviour
{
private Rigidbody _rb;
[SerializeField] private float _forceValue;
[SerializeField] private float _torqueValue;

void Start()
{
_rb = GetComponent<Rigidbody>();
_rb.AddRelativeForce(0f, _forceValue, _forceValue, ForceMode.Impulse);
_rb.AddRelativeTorque(transform.right * _torqueValue, ForceMode.Impulse);
}
}

And here’s the result:

--

--