How to add force in Unity

In this tutorial let’s see how to add force to an object in Unity and understand the type of forces that you can use.

Adding force in Unity requires a Rigidbody. So, add a Rigidbody component before adding your force script.

Addforce in Unity takes two inputs. The first one is a vector3 that tells Unity the direction of force. The second input is the type of force to be applied.

We have a cube in the scene. Let’s try to add a force to it in the forward direction.

  1. Add Rigidbody component to the cube.
  2. Make sure the Rigidbody is not set to Kinematic.
  3. Create a new script and add it to the cube.
  4. Copy and paste the below code to the script.
  5. Play the game and a force will get applied on the start of the game.
using UnityEngine;

public class unity_addforce : MonoBehaviour
{
    Rigidbody rig;
    // Start is called before the first frame update
    void Start()
    {
        rig=GetComponent<Rigidbody>();
        rig.AddForce(Vector3.forward,ForceMode.Impulse);
        
    }

    
}

Remember the following things

  1. The magnitude of force is equal to the magnitude of the vector in AddForce.
  2. If your force magnitude is less compared to the mass of the object then the object will not move.
  3. Make sure the Rigidbody is not marked as Kinematic.
  4. Dampening of the force is due to the external factors like gravity, mass and drag. You need to increase your force magnitude if the object is not moving.

Output

Modes of acceleration

  1. Acceleration- You can set an acceleration mode to the cube.
  2. Force- Works similar to impulse but applies a continuous force.
  3. Change velocity- Sets the velocity of the object to new value.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.