ForceMode 添加力
ForceMode介绍 计算公式:Ft = mv(t) 即 v(t) = Ft/m
1.ForceMode.Force : 持续施加一个力,与重力mass有关,t = 每帧间隔时间,m = mass
2.ForceMode.Impulse : 瞬间施加一个力,与重力mass有关,t = 1.0f,m = mass
3.ForceMode.Acceleration:持续施加一个力,与重力mass无关,t = 每帧间隔时间,m = 1.0f
4.ForceMode.VelocityChange:瞬间施加一个力,与重力mass无关,t = 1.0f,m = 1.0f
3.刚体的 位置 旋转 速度
Collision Detection碰撞检测模式
Discrete 离散碰撞检测(默认,适用于大部分刚体,这个肯定是最省资源的,毫无疑问,一般情况下默认这个,
不要改)
Continuous 连续碰撞检测(适用于被高速撞击的物体)
Continuous Dynamic 连续动态碰撞检测(适用于高速移动的物体)
Continuous Speculative 连续扫描碰撞检测(能够确保快速的移动的物体与物体进行碰撞,而不是穿过这个物体) 举个例子,三维弹球的跷跷板,快速碰撞有可能穿模而无法碰撞
1.给物体添加一个力
public Rigidbody Cube;
2.给物体添加一个旋转力
public Rigidbody Cube2;
4.击退怪物
public GameObject Bullet;
5.刚体的移动
public Rigidbody Cap;//胶囊体
Start is called before the first frame update
void Start()
{
取到了子弹的模板
Bullet = Resources.Load("Prefabs/Bullet") as GameObject;
7.刚体的休眠
Cap.Sleep();//强制一个刚体休眠至少一帧。一个常见的用途是从Awake中调用它以便使刚体在启用的时
候休眠
}
Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A))
{
给Cube添加一个力
Cube.AddForce(-Cube.transform.right * 20f, ForceMode.Force);
}
if (Input.GetKey(KeyCode.D))
{
给Cube添加一个力
Cube.AddForce(Cube.transform.right * 20f, ForceMode.Force);
}
if (Input.GetKey(KeyCode.W))
{
给Cube添加一个力
Cube.AddForce(-Cube.transform.forward * 20f, ForceMode.Force);
}
if (Input.GetKey(KeyCode.S))
{
给Cube添加一个力
Cube.AddForce(Cube.transform.forward * 20f, ForceMode.Force);
}
2.添加一个旋转力
if (Input.GetKey(KeyCode.Q))
{
Cube2.AddTorque(-Cube2.transform.up * 20f, ForceMode.Force);
}
if (Input.GetKey(KeyCode.E))
{
Cube2.AddTorque(Cube2.transform.up * 20f, ForceMode.Force);
}
3.刚体的 位置 旋转 速度
Debug.Log("位置:" + Cube.position + "旋转:" + Cube.rotation + "速度:" + Cube.velocity);
4.击退怪物
if (Input.GetMouseButtonDown(0))
{
GameObject go = Instantiate(Bullet);
go.transform.parent = transform;
go.transform.position = Cube.position;
go.transform.eulerAngles = Cube.transform.eulerAngles;
go.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
}
5.刚体的移动 6.刚体的旋转
float h = Input.GetAxis("Horizontal");//水平轴
float v = Input.GetAxis("Vertical");//垂直轴
Vector3 dir = new Vector3(h, 0, v);//位移量
Cap.MovePosition(Cap.position + dir * 1f);//胶囊体的位移
float x = Input.GetAxis("Mouse X");//鼠标x轴的变化量
float y = Input.GetAxis("Mouse Y");//鼠标y轴的变化量
Vector3 rot = new Vector3(Cap.transform.localEulerAngles.x - y, Cap.transform.
localEulerAngles.y + x, 0);//得到欧拉角的变化量
Quaternion quaternion = Quaternion.Euler(rot);//再将欧拉角的变化量转换成四元数的变化量
Cap.MoveRotation(quaternion);//用胶囊提的旋转方法旋转
}