#7 AddForce Method in Unity. RigidBody Class

Introduction

In this arti­cle we are going to study how to use the AddForce method of Unity's Rigid­Body class, which allows us to apply forces to GameOb­jects that have a Rigid­Body com­po­nent assigned to them. The goal is to make the ball in the GameDe­vLab bounce indef­i­nite­ly for the dura­tion of the game.

Go to the project’s main page

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

Procedure

We are going to use the "AddForce" sta­tion, which con­sists of a dia­mond-shaped con­tain­er with a ball inside. The objec­tive is to make the ball bounce indef­i­nite­ly inside the sta­tion. To do this we must apply a force when the ball hits the bottom. 

Scene of a project in Unity3D, AddForce station to study how to apply forces to GameObjects with RigidBody component.
Fig. 1: GameDe­vLab AddForce Station.

In the hier­ar­chy we have the GameOb­ject "#7 AddForce" that con­tains all the ele­ments of the sta­tion (fig­ure 2). This GameOb­ject is also assigned the Script "AddForce­ToOb­ject".

Hierarchy of a project in Unity3D, highlighted is the GameObject "AddForce" that is used to study how to apply forces to GameObjects with RigidBody component.
Fig. 2: Hier­ar­chy of the GameDe­vLab. Let's work with the GameOb­ject "#7 AddForce".

Inspector of a project in Unity3D, the component "AddForceToObject" is observed that will be in charge of applying a force on the GameObject.
Fig. 3: Com­po­nente "AddForce­ToOb­ject" asig­na­da al GameOb­ject "#7 AddForce".

In fig­ure 3 we see the "AddForce­ToOb­ject" com­po­nent in the inspec­tor. We can assign an object to which to apply the force, in this case is "Sphere" the ball that is inside the sta­tion. We can assign a min­i­mum and max­i­mum force val­ue to apply to the ball and the direc­tion of the force.

To the left, the sphere of the station "AddForce". On the right, its components are displayed in the inspector. Between them there is a RigidBody component that will give you physical behavior.
Fig. 4: To the left, the sphere of the "AddForce" sta­tion. On the right, its com­po­nents are dis­played in the inspec­tor. Between them there is a Rigid­Body com­po­nent that will give you phys­i­cal behavior.

The Ball GameOb­ject is sim­ply a prim­i­tive sphere made in Uni­ty, it is assigned a Rigid­Body com­po­nent that gives it phys­i­cal behav­ior. The ball will be affect­ed by grav­i­ty, it will be able to col­lide with objects that Col­lid­ers have assigned and we will be able to apply forces to it.

On the left the GameObject in charge of detecting if the ball touches the ground. To the right its components in the inspector.
Fig. 5: To the left the GameOb­ject in charge of detect­ing if the ball touch­es the ground. To the right its com­po­nents in the inspector.

At the base of the struc­ture we have a cylin­der-shaped GameOb­ject that has a Box Col­lid­er assigned in Trig­ger mode (fig­ure 4). It also has a script called "AddForceCol­li­sion­De­tec­tion" that will detect the ball.

Solution

When we open the "AddForce­ToOb­ject" Script for the first time we find what you see in fig­ure 6. All the para­me­ters you see in the inspec­tor are defined and it con­tains only one method called "addForce". This method is defined as pub­lic because it is the one that is going to be called when it is detect­ed that the ball has touched the ground of the structure.

There is a C# script with the name "AddForceToObject". The Script corresponds to a project in Unity3d, which studies how to apply forces to GameObjects.
Fig. 6: Script "AddForce­ToOb­ject" incompleto.

The solu­tion of this exer­cise is shown below and the pur­pose of each instruc­tion is explained.

There is a method in C# language with the name "addForce". The method corresponds to a project in Unity3d, which studies how to apply forces to GameObjects.
Fig. 7: Solu­tion of the exercise.

To solve this prob­lem we will require three instruc­tions. The first will define the mag­ni­tude of the force to be applied, which will be a ran­dom val­ue between the min­i­mum and max­i­mum forces.

The sec­ond instruc­tion will define the total force to apply, if you saw the video about physics, to define a force we need a mag­ni­tude and a direc­tion. For the direc­tion we use the vec­tor of three dimen­sions (class Vector3) that is seen in the inspec­tor, it is called "forceDi­rec­tion­Vec­tor". But we are not going to use it as it is writ­ten in the inspec­tor. To pre­vent that Vector3 from affect­ing the mag­ni­tude of the force we must nor­mal­ize it. We do this with the "Nor­mal­ize" method of the Vector3 class. The total force to apply will be a Vector3 called "force" which is the result of mul­ti­ply­ing the direc­tion by the magnitude.

Final­ly the third instruc­tion con­sists in apply­ing force on the object. The forces are applic­a­ble to GameOb­jects that have a Rigid­Body com­po­nent assigned, in our case we are going to apply the force on the Rigid­Body com­po­nent of the ball. We use the "Get­Com­po­nent" method to obtain the Rigid­Body com­po­nent and final­ly we exe­cute the AddForce method, pass­ing as para­me­ter the force to apply.

Scene of a project in Unity3D, AddForce station to study how to apply forces to GameObjects with RigidBody component.
Fig. 8: Sta­tion run­ning. The ball bounces with vari­able force.

Conclusion

When test­ing the game, the ball rebounds indef­i­nite­ly with a ran­dom force.

To define a force we need a real val­ue for the mag­ni­tude and a vec­tor for the direc­tion in which it should be applied.

The forces act on the Rigid­Body com­po­nent of a GameObjects.

Scroll to Top
Secured By miniOrange