RIgidbody组件
常用属性
Mass:质量 最小值0.00000001 最大值10000000000
Drag:阻力 最小值0 最大值无限
Angular Drag:角阻力 最小值0 最大值无限
Use Gravity:使用重力
is Kinematic:物理是否影响该物体(勾选时,不影响)
Interpolate:一个以固定的帧率平滑物理运行的插值(选择Interpolate时物理移动更平滑)
Collision Detection:碰撞检测 Discreate:离散的,快速移动的物体可能不会发生碰撞; Continuous:连续的,适用于被快速移动的物体碰撞的物体; Continuous Dynamic:动态连续的,适用于快速移动的物体
Constraints:限制 Freeze Position:在某个轴向上无法移动 Freeze Rotation:在某个轴向上无法旋转
常用方法
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rigidbodyTest : MonoBehaviour {
private Rigidbody _rigidbody;
void Start ()
{
_rigidbody = transform.GetComponent<Rigidbody>();
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Q))
{
_rigidbody.velocity = new Vector3(1,1,1);
}
if (Input.GetKeyDown(KeyCode.W))
{
_rigidbody.Sleep();
}
if (Input.GetKeyDown(KeyCode.E))
{
_rigidbody.WakeUp();
}
if (Input.GetKeyDown(KeyCode.A))
{
_rigidbody.MovePosition(new Vector3(10,100,200));
}
if (Input.GetKeyDown(KeyCode.S))
{
_rigidbody.freezeRotation = true;
}
if (Input.GetKeyDown(KeyCode.D))
{
_rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
}
if (Input.GetKeyDown(KeyCode.F))
{
_rigidbody.AddExplosionForce(50,new Vector3(0,0,0),20);
}
if (Input.GetKeyDown(KeyCode.R))
{
_rigidbody.AddForce(Vector3.forward*3,ForceMode.Acceleration);
}
if (Input.GetKeyDown(KeyCode.T))
{
_rigidbody.AddTorque(transform.forward*10);
}
}
private void OnTriggerEnter(Collider other)
{
print(11);
}
private void OnTriggerStay(Collider other)
{
print(22);
}
private void OnTriggerExit(Collider other)
{
print(33);
}
private void OnCollisionEnter(Collision other)
{
print(other.relativeVelocity);
}
}
|