Unity 关节


            关节组件可添加至多个游戏对象当中,而添加了关节的游戏对象将通过关节连接在一起并且感应连带的物理效果,但是关节必须依赖于刚体组件。比如给两个游戏对象添加了弹性关节组件,这就好比给两个模型添加了一个弹簧,当某一物体发送运动时,通过关节连带的另一个物体也将实现自由反弹效果。

        


关节介绍:


链条关节(Hinge Joint):将两个物体以链条的形式绑在一起,当力量过大超过链条的固定力矩时,两个物体就会产生相互的拉力。


固定关节(Fixed Joint):将两个物体永远以相对的位置固定在一起,即使发生物理改变,它们之间的相对位置也不会发生改变。


弹簧关节(Spring Joint):将两个物体以弹簧的形式绑在一起,挤压它们会得到向外的推力,拉伸它们会得到两边对中间的拉力。


角色关节(Character Joint):它可以模拟角色的骨骼关节,就好比人的手腕一样可以大范围任意角度旋转。


可配置关节(Character Joint):它可以模拟任意关节的效果,包括上面的4种效果,它是最强大的,也是最复杂的。


可以设置关节的断裂。使用breakForce可设置关节断裂的力,一旦力度超过它,关节将会断裂。断裂时,可在OnJonitBreakForce(float breakForce)方法中jiant


如何添加关节:  在菜单栏中选"Compoent"->"Physics"菜单项->选择要添加的关节。



using UnityEngine;
using System.Collections;

public class Script_06_10 : MonoBehaviour 
{
	//链接关节游戏对象
	GameObject connectedObj = null;
	//当前链接的关节组件
	Component jointComponent = null;
	
	void Start()
	{   
		 //获得链接关节的游戏对象
		 connectedObj = GameObject.Find("Cube1");
	}

	void OnGUI()
	{
		if(GUILayout.Button("添加链条关节"))
		{
			
			ResetJoint();
			jointComponent = gameObject.AddComponent("HingeJoint");
			HingeJoint hjoint = (HingeJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			hjoint.connectedBody = connectedObj.rigidbody;
		}
		
		if(GUILayout.Button("添加固定关节"))
		{
			ResetJoint();
			jointComponent =gameObject.AddComponent("FixedJoint");
			FixedJoint fjoint = (FixedJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			fjoint.connectedBody = connectedObj.rigidbody;
		}
		
		if(GUILayout.Button("添加弹簧关节"))
		{
			ResetJoint();
			jointComponent =gameObject.AddComponent("SpringJoint");
			SpringJoint sjoint = (SpringJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			sjoint.connectedBody = connectedObj.rigidbody;
		}
		
		if(GUILayout.Button("添加角色关节"))
		{
			ResetJoint();
			jointComponent =gameObject.AddComponent("CharacterJoint");
			CharacterJoint cjoint = (CharacterJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			cjoint.connectedBody = connectedObj.rigidbody;
		}
		
		if(GUILayout.Button("添加可配置关节"))
		{
			ResetJoint();
			jointComponent =gameObject.AddComponent("ConfigurableJoint");
			ConfigurableJoint cojoint = (ConfigurableJoint)jointComponent;
			connectedObj.rigidbody.useGravity = true;
			cojoint.connectedBody = connectedObj.rigidbody;
		}
	}
	
	//重置关节
	void ResetJoint(){
		//销毁之前添加的关节组件
		Destroy (jointComponent);
		//重置对象位置
		this.transform.position = new Vector3(821.0f,72.0f,660.0f);
		connectedObj.gameObject.transform.position = new Vector3(805.0f,48.0f,660.0f);
		//不敢应重力
		connectedObj.rigidbody.useGravity = false;
	}
}




你可能感兴趣的:(Unity3D,Unity3D)