Unity3d基础学习第4天

物理引擎

1.刚体

1.1 Rigibody属性

1.Mass质量 默认值1,大部分物理的Mass为0.1 ~10 float
2.Drag 阻力 任意方向都会收到阻力 float
3.Angular Drag 旋转阻力 float
4.Use Gravity 使用重力
5.Is Kinematic 是否遵循动力学
6.Interpolate 差值方式
7.Collision Detection 碰撞检测模式 防止速度过快而产生击穿现象
8.Constrains 约束条件 冰冻位置和旋转

1.2 Rigibody使用方法

1.2.1 添加位移速度

    this.GetComponent<Rigidbody> ().velocity =  Vector3.left * move; 

1.2.2 降低重心

    this.GetComponent<Rigidbody> ().centerOfMass = Vector3.down * 0.5f;  //  0 -0.5 0

1.2.3 碰撞检测开关

    this.GetComponent<Rigidbody> ().detectCollisions = false; 

1.2.4 添加旋转角速度

    this.GetComponent<Rigidbody> ().angularVelocity = Vector3.up * speed;

1.2.5 添加力

    this.GetComponent<Rigidbody> ().AddForce (Vector3.up * 400f);

1.ForceMode默认force 施加力时,受重力影响 慢慢加力
2.ForceMode.Acceleration 忽略本身质量。默认值1
3.ForceMode.VelocityChange 忽略质量
4.ForceMode.Impulse 以冲量的方式添加力 瞬间的作用力.

1.2.6 指定位置添加力

    this.GetComponent<Rigidbody>().AddForceAtPosition(Vector3.up*400f,transform.position +new Vector3 (0.5f, 0, 0.5f));

2.碰撞器

2.1 分类

1.盒子碰撞器
2.球体碰撞器
3.胶囊碰撞器
4.网络碰撞器
5.车轮碰撞器
6.地形碰撞器

2.2 使用方法

2.2.1 忽略碰撞

    public Collider red;
	public Collider blue;
	void Start () 
	{Physics.IgnoreCollision (red, blue,true);  //忽略碰撞}

2.2.2 碰撞的方法

    void OnCollisionEnter(Collision g)  //开始碰撞
	{
		Debug.Log ("开始碰撞"+g.gameObject.name);
	}
	void OnCollisionStay(Collision g)  //碰撞中
	{
		Debug.Log ("碰撞中"+g.gameObject.name);
	}
	void OnCollisionExit(Collision g)  //碰撞结束
	{
		Debug.Log ("碰撞结束" +g.gameObject.name);
	}

2.2.3 触发方法 在collider中Is Trigger 中勾选

    void OnTriggerEnter(Collider g)
	{
		Debug.Log ("触发开始" + g.gameObject.name);
	}
	void OnTriggerStay(Collider g)
	{
		Debug.Log ("触发中" + g.gameObject.name);
	}
	void OnTriggerExit(Collider g)
	{
		Debug.Log ("触发结束" + g.gameObject.name);
	}

猜你喜欢

转载自blog.csdn.net/JingDuiTell/article/details/88787608
今日推荐