示例一:通过给物体添加力矩,让物体旋转
using System.Collections;
using UnityEngine;
public class Test2 : MonoBehaviour {
[SerializeField]
private Rigidbody m_Rigidbody;
void Awake()
{
if (null == m_Rigidbody)
m_Rigidbody = this.GetComponent<Rigidbody>();
}
void Start () {
/*
* ForceMode.Force 添加一个可持续力到刚体
* ForceMode.Impulse 添加一个瞬间冲击力到刚体
* ForceMode.Acceleration 添加一个可持续加速度到刚体,忽略它的质量。
* ForceMode.VelocityChange 添加一个瞬间速度变化给刚体,忽略它的质量。
* 参见: http://www.ceeger.com/Script/Enumerations/ForceMode/ForceMode.html
*/
//让物体绕世界空间的Y轴旋转
m_Rigidbody.AddTorque(Vector3.up, ForceMode.Impulse);
}
}
运行测试