unity add force code example

Example 1: unity how to add force

public Rigidbody rb;

if(Input.GetKey(KeyCode.W))
{
	rb.AddForce(100, 0, 0);
}

Example 2: how to make something addforce in the direction of something in untiy

dir = target.transform.position - transform.position;
dir = dir.normalized;
rigidbody.AddForce(dir * force);

Example 3: Rigidbody.addforce

using UnityEngine;public class ExampleClass : MonoBehaviour
{
    public float thrust = 1.0f;
    public Rigidbody rb;    void Start()
    {
        rb = GetComponent<Rigidbody>();
        rb.AddForce(0, 0, thrust, ForceMode.Impulse);
    }
}

Example 4: how to make rb.addforce 2d

public Rigidbody rb; //make reference in Unity Inspector

public float SpeedX = 0.1f;
public float SpeedY = 0.5f;

rb.AddForce(new Vector2(SpeedX, SpeedY));

Example 5: unity rigidbody addforce

rb = GetComponent<Rigidbody>();
rb.AddForce(new Vector3(1.5f,1.5f,1.5f));

Example 6: how to add a force to an object unity

private Rigidbody2D rb;
public Vector2 direction;
public float force;

void Start()
{
	rb = GetComponent<Rigidbody2D>();
    rb.AddForce(direction * force, ForceMode2D.Impulse);
    //rb.AddForce(--pass in Vector--, --can pass in ForceMode--);
}