unity 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);
}
}
3.71
7
public class Example : MonoBehaviour
{
Rigidbody m_Rigidbody;
public float m_Thrust = 20f;
void Start()
{
//Fetch the Rigidbody from the GameObject with this script attached
m_Rigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (Input.GetButton("Jump"))
{
//Apply a force to this Rigidbody in direction of this GameObjects up axis
m_Rigidbody.AddForce(transform.up * m_Thrust);
}
}
}
Thank you!
7
0
3.6
5
public Rigidbody rb; //make reference in Unity Inspector
public float SpeedX = 0.1f;
public float SpeedY = 0.5f;
rb.AddForce(new Vector2(SpeedX, SpeedY));
Thank you!
5
0
4.5
4
rb.AddForce(0, 0, 5 * Time.deltaTime);
// Time.deltaTime is optional ( Make sure that "T" is caps)
Thank you!
4
0
3.8
5
m_Rigidbody.AddForce(transform.up * m_Thrust);
//rigidbodyToApplyForceTo.AddForce(Direction*Force);
Thank you!
5
0
3.5
8
rb = GetComponent<Rigidbody>();
rb.AddForce(new Vector3(1.5f,1.5f,1.5f));
Thank you!
8
0
Are there any code examples left?
New code examples in category C#
-
C# 2022-03-27 22:40:23 c# movement script
-
C# 2022-03-27 22:15:17 Delete last modification on EntityFramework Core
-
C# 2022-03-27 22:15:02 file.create folder c#
-
C# 2022-03-27 19:25:04 Error inflating class android.support.constraint.ConstraintLayout
-
C# 2022-03-27 19:15:02 top down shooting in unity 2D
-
C# 2022-03-27 18:50:02 c# remove invalid directory characters
-
C# 2022-03-27 18:30:07 how to use K2 games Games parallax background
-
C# 2022-03-27 18:25:10 c# replace crlf
-
C# 2022-03-27 18:25:09 keycode for left mouse click unity