rigidbody 2d addforce code example
Example 1: 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 2: how to add reference to rigidbody 2d
public Rigidbody2D rb;
Example 3: rigidbody addforce
using UnityEngine;public class Example : MonoBehaviour
{
Rigidbody m_Rigidbody;
public float m_Thrust = 20f; void Start()
{
m_Rigidbody = GetComponent<Rigidbody>();
} void FixedUpdate()
{
if (Input.GetButton("Jump"))
{
m_Rigidbody.AddForce(transform.up * m_Thrust);
}
}
}