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;// Use this for initializationvoid Start () {_rigidbody = transform.GetComponent<Rigidbody>();}// Update is called once per framevoid Update () {if (Input.GetKeyDown(KeyCode.Q)){_rigidbody.velocity = new Vector3(1,1,1); //给其一个速度矢量// _rigidbody.position = new Vector3(5,5,5); //给其一个位置}if (Input.GetKeyDown(KeyCode.W)){_rigidbody.Sleep();//强制性使刚体休眠,不动了;休眠是性能优化的一个措施,物理引擎不会处理处于休眠状态的刚体;//刚体在以下情况会被唤醒:1,其他刚体碰撞器作用于休眠刚体。2,被其他刚体通过移动的关节连接//3,修改了刚体的属性。4,添加外力时}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; //开启則 刚体的XYZ轴全部冻结}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); //沿着某一方向添加一个扭矩}//_rigidbody.transform.Rotate(transform.up, Time.deltaTime); //基于transform的旋转//_rigidbody.angularVelocity = transform.right * Time.deltaTime;//基于刚体的旋转}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); //两个碰撞物体的相对线性速度}
}