- 最后登录
- 2017-5-15
- 注册时间
- 2012-3-1
- 阅读权限
- 90
- 积分
- 32973
- 纳金币
- 32806
- 精华
- 12
|
代码清单如下: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 = ***e;
hjoint.connectedBody = connectedObj.rigidbody;
}
if(GUILayout.Button("添加固定关节"))
{
ResetJoint();
jointComponent =gameObject.AddComponent("FixedJoint");
FixedJoint fjoint = (FixedJoint)jointComponent;
connectedObj.rigidbody.useGravity = ***e;
fjoint.connectedBody = connectedObj.rigidbody;
}
if(GUILayout.Button("添加弹簧关节"))
{
ResetJoint();
jointComponent =gameObject.AddComponent("SpringJoint");
SpringJoint sjoint = (SpringJoint)jointComponent;
connectedObj.rigidbody.useGravity = ***e;
sjoint.connectedBody = connectedObj.rigidbody;
}
if(GUILayout.Button("添加角色关节"))
{
ResetJoint();
jointComponent =gameObject.AddComponent("CharacterJoint");
CharacterJoint cjoint = (CharacterJoint)jointComponent;
connectedObj.rigidbody.useGravity = ***e;
cjoint.connectedBody = connectedObj.rigidbody;
}
if(GUILayout.Button("添加可配置关节"))
{
ResetJoint();
jointComponent =gameObject.AddComponent("ConfigurableJoint");
ConfigurableJoint cojoint = (ConfigurableJoint)jointComponent;
connectedObj.rigidbody.useGravity = ***e;
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;
}
} |
|