纳金网

标题: u3d13关节组件链条、固定、弹簧、角色、可配置关节 [打印本页]

作者: 她。    时间: 2012-12-29 10:52
标题: u3d13关节组件链条、固定、弹簧、角色、可配置关节
代码清单如下: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;
}
}
作者: 她。    时间: 2012-12-29 10:54
如果觉得好的话就赞一个吧~
作者: 狂风大尉    时间: 2012-12-31 02:43
谢谢楼主的帖子分享,学习了

作者: libufan    时间: 2013-8-14 10:06
谢谢楼主分享
作者: libufan    时间: 2013-8-14 10:06
有没有模拟真是世界的弹簧效果?
作者: 查穆1    时间: 2013-8-16 10:38
感谢分享
作者: dang050    时间: 2017-8-30 15:02
好资源哦,顶一下
作者: 673304426    时间: 2017-8-30 16:38
赞一个 哈哈哈哈




欢迎光临 纳金网 (http://c-www.narkii.com/club/) Powered by Discuz! X2.5