using UnityEngine;
public class ExampleScript : MonoBehaviour
{
public Rigidbody myRigidbody;
public float myThrust = 20f;
private void Start()
{
myRigidbody = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
if (Input.GetButton("Jump"))
{
// 对这个物体施加向上的力
myRigidbody.AddForce(transform.up * myThrust);
}
}
}
示例:指定 x、y、z 各轴的大小
using UnityEngine;
public class Example : MonoBehaviour
{
public Rigidbody myRigidbody;
public float myThrust = 10f;
private void Start()
{
myRigidbody = GetComponent<Rigidbody>();
// 给物体一个沿 z 轴方向的瞬时力
myRigidbody.AddForce(0, 0, myThrust, ForceMode.Impulse);
}
}