C# Addforce Unity Code Example


Example 1: unity how to add force


public Rigidbody rb;

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

Example 2: 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 3: 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));

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable