Unity Jonit(关节)

前言:

      学了这么久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>

最后的最后

       呵呵,写的很简单,但是只是为了了解其用法而已,具体每个关节的属性,大家可以上官网去学习下,然后自己再动手写下,最后可以做个小案例熟悉起每个属性的用法即可!

 



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值