一、Rigidbody 是什么?
通过物理模拟控制一个物体的位置。控制物体的位置 - 它使物体在重力影响下下落,并可计算物体将怎样响应碰撞。
二、关于Rigidbody的变量
1.Rigidbody.velocity 速度
刚体的速度向量。
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.velocity = new Vector3(0, 10, 0);
}
}
2.Rigidbody.drag 阻力
物体的阻力。
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.drag = 20;
}
}
3.Rigidbody.mass 质量
物体的质量。
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.mass= 20;
}
}
4.Rigidbody.position 位置
刚体的位置。
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.position = Vector3.zero;
}
}
5.Rigidbody.rotation 旋转
刚体的旋转。
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.rotation = Quaternion.identity;
}
}
三、关于Rigidbody的函数
1.Rigidbody.SetDensity 设置密度
基于附加的碰撞器假设一个固定的密度设置质量。这个可以用来设置质量,随着碰撞器的尺寸而缩放。
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.SetDensity(1.5f);
}
}
2.Rigidbody.AddForce 添加力
添加一个力到刚体。作为结果刚体将开始移动。
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.AddForce(Vector3.up * 10);
}
}
3.Rigidbody.MovePosition 移动位置
移动刚体到position。
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.MovePosition(Vector3.up * 10);
}
}
4.Rigidbody.MoveRotation 移动旋转
旋转刚体到rotation。
public Vector3 eulerAngleVelocity = new Vector3(0, 100, 0);
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Quaternion deltaRotation = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime);
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.MoveRotation(rgy.rotation * deltaRotation);
}
}
5.Rigidbody.Sleep 休眠
强制一个刚体休眠至少一帧
private Collision collision;
public void Awake()
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.Sleep();
}
6.Rigidbody.Sleep 休眠
强制唤醒一个刚体。
private Collision collision;
public void Awake()
{
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.WakeUp();
}
四、关于碰撞的三个状态
Rigidbody.OnCollisionEnter 当碰撞进入 Rigidbody.OnCollisionExit 当碰撞退出 Rigidbody.OnCollisionStay 当碰撞停留
public Vector3 eulerAngleVelocity = new Vector3(0, 100, 0);
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "Chessman")
{
Quaternion deltaRotation = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime);
Rigidbody rgy = collision.collider.GetComponent<Rigidbody>();
rgy.MoveRotation(rgy.rotation * deltaRotation);
}
}
用法类似。
|