• 【Unity 3D】学习笔记四十一:关节


    关节

    关节组件能够加入至多个游戏对象中,而加入关节的游戏对象将通过关节连接在一起而且感觉连带的物理效果。须要注意的是:关节必须依赖于刚体组件。


    关节介绍

    关节一共分为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;
    		}
    	}
    	
    	//重置关节
    	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;
    	}
    }
    
    执行后:

    点击左边button,两个正方体便会下落实现不同的关节效果。主要是上述的代码非常值得參考与细读。

  • 相关阅读:
    nodejs生成UID(唯一标识符)——node-uuid模块
    python 3.6 关于python的介绍
    Hdu-3333 Turning Tree (离线树状数组/线段树)
    HDU-3333 Turing Tree 分块求区间不同数和
    Hello,World.
    IntelliJ IDEA 自动化工具安装并添加自动化测试框架
    git&sourcetree安装及在IntelliIJ下拉取项目基础使用
    3.jquery在js文件中获取选择器对象
    2.jquery在js中写标准的ajax请求
    操作系统简介
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4298301.html
Copyright © 2020-2023  润新知