前言:
学了这么久Unity,刚好可以复习下前面的知识,这里我就记下Unity的五种关节运用,方便以后复习的时候也可以看下。
进入主题:
这里将会记录下Unity的五种关节的说明和使用方法。
Hinge Joint(铰链关节)
将两个物体以链条的形式绑在一起,当力量大于链条的固定力矩时,两个物体就会产生相互的拉力,同时铰链关节是由两个刚体组成,约束它们像连在一个铰链上一样运动,适用于门,不过对于典型的链子、钟摆等同样适用。
Fixed Joint(固定关节)
将两个物体永远以相对的位置固定在一起,即使发生物理改变,它们之间的相对位置也将不变,同时固定关节基于另一个物体来限制一个物体的运动。效果类似于父子关系,但是不是通过层级变换,而是通过物理实现的。使用它的最佳情境是当你有一些想要轻易分开的物体,或想让两个没有父子关系的物体一起运动。
spring joint(弹簧关节)
将两个物体以弹簧的形式绑定在一起,挤压它们会得到向外的力,拉伸它们将得到向里的力,注意弹簧关节组连接两个刚体,让它们像被弹簧连接着一样运动。
character joint(角色关节)
角色关节主要用于实现布娃娃效果。角色关节是扩展的球关节,可以用于限制关节在不同旋转轴下的旋转角度。
configurable joint(可配置关节)
可以模拟任意关节的效果,同时可配置关节将PhysX引擎中所有与关节相关的属性都设置为可配置的,因此可以用此组件创造出与其他关节类型行为相似的关节。
注意:
使用Break Force可设置关节断裂的力,一旦力超过它,关节将会断裂。断裂时,通过onjointbreakforce方法可监听相关事件。
using UnityEngine;
<span style="font-family:Microsoft YaHei;font-size:14px;color:#660000;">using System.Collections;
public class Joint : MonoBehaviour
{
/// <summary>
/// 关节组件
/// </summary>
Component jointComponent = null;
/// <summary>
/// 要连接物体的刚体组件
/// </summary>
Rigidbody connectedObjComponent = null;
/// <summary>
/// 自身的刚体组件
/// </summary>
Rigidbody rigidbodyCompent = null;
void Start()
{
connectedObjComponent = GameObject.FindWithTag("ConnectObj").rigidbody;
rigidbodyCompent = gameObject.rigidbody;
connectedObjComponent.useGravity = false;
rigidbodyCompent.useGravity = false;
}
/// <summary>
/// GUI事件
/// </summary>
void OnGUI()
{
if (GUILayout.Button("添加链条关节"))
{
ResetJoint();
jointComponent = gameObject.AddComponent("HingeJoint");
HingeJoint hjoint = (HingeJoint)jointComponent;
hjoint.connectedBody = connectedObjComponent;
connectedObjComponent.useGravity = true;
rigidbodyCompent.useGravity = true;
}
if (GUILayout.Button("添加固定关节"))
{
ResetJoint();
jointComponent = gameObject.AddComponent("FixedJoint");
FixedJoint fjoint = (FixedJoint)jointComponent;
connectedObjComponent.useGravity = true;
rigidbodyCompent.useGravity=true;
fjoint.connectedBody = connectedObjComponent;
}
if (GUILayout.Button("添加弹簧关节"))
{
ResetJoint();
jointComponent = gameObject.AddComponent("SpringJoint");
SpringJoint sjoint = (SpringJoint)jointComponent;
connectedObjComponent.useGravity = true;
rigidbodyCompent.useGravity = true;
sjoint.connectedBody = connectedObjComponent;
}
if (GUILayout.Button("添加角色关节"))
{
ResetJoint();
jointComponent = gameObject.AddComponent("CharacterJoint");
CharacterJoint cjoint = (CharacterJoint)jointComponent;
connectedObjComponent.useGravity = true;
rigidbodyCompent.useGravity = true;
cjoint.connectedBody = connectedObjComponent;
}
if (GUILayout.Button("添加可配置关节"))
{
ResetJoint();
jointComponent = gameObject.AddComponent("ConfigurableJoint");
ConfigurableJoint cojoint = (ConfigurableJoint)jointComponent;
connectedObjComponent.useGravity = true;
rigidbodyCompent.useGravity = true;
cojoint.connectedBody = connectedObjComponent;
}
}
/// <summary>
/// 重置关节
/// </summary>
void ResetJoint()
{
//销毁之前添加的关节组件,当然这里你可以不销毁,可以使用jointComponent.active=false,那前面的代码也需变动下即可.
Destroy(jointComponent);
//位置复原
this.transform.position = new Vector3(2.431f, 8.388f, -7.374f);
connectedObjComponent.transform.position = new Vector3(0.604f, 7.388f, -7.374f);
//取消使用重力
connectedObjComponent.useGravity = false;
rigidbodyCompent.useGravity = false;
}
}</span><strong style="color: rgb(153, 0, 0);font-size:18px;">
</strong>
呵呵,写的很简单,但是只是为了了解其用法而已,具体每个关节的属性,大家可以上官网去学习下,然后自己再动手写下,最后可以做个小案例熟悉起每个属性的用法即可!