• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

U3D中关节(joint)运用

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0403/438.html

U3D中关节(joint)运用

 
 
关节
 
关节组件能够添加至多个游戏对象中,而添加关节的游戏对象将经过关节衔接在一同并且觉得连带的物理效果。常州手游开发需求留意的是:关节必需依赖于刚体组件。
 
关节引见
 
关节一共分为5大类:链条关节,固定关节,弹簧关节,角色关节和可配置关节。本篇文章就简单引见这些组件的含义。
 
链条关节(hinge joint):将两个物体以链条的方式绑在一同,当力气大于链条的固定力矩时,两个物体就会产生互相的拉力。
 
固定关节(fixed joint):将两个物体永远以相对的位置固定在一同,即便发作物理改动,它们之间的相对位置也将不变。
 
弹簧关节(spring joint):将两个物体以弹簧的方式绑定在一同,挤压它们会得到向外的力,拉伸它们将得到向里的力。
 
角色关节(character joint):能够模仿角色的骨骼关节。
 
可配置关节(configurable joint):能够模仿恣意关节的效果。
 
关节是一个游戏组件,在导航菜单栏当选择component——physics然后从当选择一种关节组件,即可完成关节组件的添加。
 
运用breakforce可设置关节断裂的力,一旦力超越它,关节将会断裂。断裂时,经过onjointbreakforce办法可监听相关事情。
 
下面常州游戏开发培训给个实例:
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;
        }
    }
    //手机App外包重置关节
    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;
    }
 

上篇:上一篇:常州手游开发-UGUI屏幕自顺应
下篇:下一篇:Unity中Camera.ScreenToWorldPoint运用解析