Exemplo n.º 1
0
 void GetParts(Transform t)
 {
     foreach (Transform child in t)
     {
         GetParts(child);
         if (ShouldIgnoreThis(child.gameObject))
         {
             continue;
         }
         Collider col = child.GetComponent <Collider> ();
         if (col != null)
         {
             CharacterJoint joint = child.GetComponent <CharacterJoint> ();
             if (joint != null)
             {
                 joint.enableProjection = enableProjection;
             }
             if (!col.isTrigger)
             {
                 colliders.Add(col);
                 Rigidbody rb = child.GetComponent <Rigidbody> ();
                 if (rb != null)
                 {
                     rigidbodys.Add(rb);
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
    void AddJoint(Transform target, Rigidbody connectedBody, bool connectToSpace)
    {
        end = target.gameObject.AddComponent <CharacterJoint>();
        if (!connectToSpace)
        {
            end.connectedBody = connectedBody;
        }
        else
        {
            //connect to nothing aka space(lock in air)
        }
        end.swingAxis = swingAxis;
        SoftJointLimit limit_setter = end.lowTwistLimit;

        limit_setter.limit = lowTwistLimit;
        end.lowTwistLimit  = limit_setter;
        limit_setter       = end.highTwistLimit;
        limit_setter.limit = highTwistLimit;
        end.highTwistLimit = limit_setter;
        limit_setter       = end.swing1Limit;
        limit_setter.limit = swing1Limit;
        end.swing1Limit    = limit_setter;
        target.parent      = transform;
        //end.enableProjection = true;
    }
Exemplo n.º 3
0
        private void BuildJoints()
        {
            foreach (BoneData bone in m_Bones)
            {
                if (bone.Parent == null)
                {
                    continue;
                }

                CharacterJoint joint = Undo.AddComponent <CharacterJoint>(bone.Anchor.gameObject);
                bone.Joint = joint;

                joint.axis          = CalculateDirectionAxis(bone.Anchor.InverseTransformDirection(bone.Axis));
                joint.swingAxis     = CalculateDirectionAxis(bone.Anchor.InverseTransformDirection(bone.NormalAxis));
                joint.anchor        = Vector3.zero;
                joint.connectedBody = bone.Parent.Anchor.GetComponent <Rigidbody>();

                SoftJointLimit limit = new SoftJointLimit();

                limit.limit         = bone.MinLimit;
                joint.lowTwistLimit = limit;

                limit.limit          = bone.MaxLimit;
                joint.highTwistLimit = limit;

                limit.limit       = bone.SwingLimit;
                joint.swing1Limit = limit;

                limit.limit            = 0;
                joint.swing2Limit      = limit;
                joint.enableProjection = m_EnableProjection;
            }
        }
Exemplo n.º 4
0
    void FistRestricting()
    {
        Transform arm   = transform.parent.parent;
        Transform elbow = transform.parent;

        CharacterJoint armJoint   = arm.GetComponent <CharacterJoint>();
        CharacterJoint elbowJoint = elbow.GetComponent <CharacterJoint>();
        CharacterJoint handJoint  = GetComponent <CharacterJoint>();

        SoftJointLimit armLowLimit     = armJoint.lowTwistLimit;
        SoftJointLimit armHighLimit    = armJoint.highTwistLimit;
        SoftJointLimit elbowLowLimit   = elbowJoint.lowTwistLimit;
        SoftJointLimit elbowHighLimit  = elbowJoint.highTwistLimit;
        SoftJointLimit handSwing2Limit = handJoint.swing2Limit;

        armLowLimit.limit     = 0;
        armHighLimit.limit    = 0;
        elbowLowLimit.limit   = -90;
        elbowHighLimit.limit  = 0;
        handSwing2Limit.limit = 0;

        armJoint.lowTwistLimit    = armLowLimit;
        armJoint.highTwistLimit   = armHighLimit;
        elbowJoint.lowTwistLimit  = elbowLowLimit;
        elbowJoint.highTwistLimit = elbowHighLimit;
        handJoint.swing2Limit     = handSwing2Limit;
    }
    void OnSceneGUI()
    {
        //Debug.Log(this.target.GetType());
        EnhanceCharacterJoint script = (EnhanceCharacterJoint)this.target;

        if (script != null)
        {
            CharacterJoint joint = (CharacterJoint)script.gameObject.GetComponent(typeof(CharacterJoint));

            Vector3 twistAxis  = joint.transform.TransformDirection(joint.axis).normalized;
            Vector3 swing1Axis = joint.transform.TransformDirection(joint.swingAxis).normalized;
            Vector3 swing2Axis = Vector3.Cross(twistAxis, swing1Axis);

            var   position = joint.transform.position;
            float size     = HandleUtility.GetHandleSize(position);

            Handles.color = new Color(1, 0.5f, 0); // orange == twist, direction = swing1, rotates about twist axis
            Handles.DrawWireArc(position, twistAxis, swing1Axis, joint.highTwistLimit.limit, size);
            Handles.DrawWireArc(position, twistAxis, swing1Axis, joint.lowTwistLimit.limit, size);
            Handles.DrawLine(position, position + Quaternion.AngleAxis(joint.highTwistLimit.limit, twistAxis) * swing1Axis * size);
            Handles.DrawLine(position, position + Quaternion.AngleAxis(joint.lowTwistLimit.limit, twistAxis) * swing1Axis * size);

            Handles.color = new Color(0, 0.3f, 0); // green=swing1, direction=twistAxis rotates about swing1Axis
            Handles.DrawWireArc(position, swing1Axis, twistAxis, joint.swing1Limit.limit, size);
            Handles.DrawWireArc(position, swing1Axis, twistAxis, -joint.swing1Limit.limit, size);
            Handles.DrawLine(position, position + Quaternion.AngleAxis(joint.swing1Limit.limit, swing1Axis) * twistAxis * size);
            Handles.DrawLine(position, position + Quaternion.AngleAxis(-joint.swing1Limit.limit, swing1Axis) * twistAxis * size);

            Handles.color = new Color(0, 0, 0.5f); // blue=swing2, direction=twistAxis, rotates about swing2 axis
            Handles.DrawWireArc(position, swing2Axis, twistAxis, joint.swing2Limit.limit, size);
            Handles.DrawWireArc(position, swing2Axis, twistAxis, -joint.swing2Limit.limit, size);
            Handles.DrawLine(position, position + Quaternion.AngleAxis(joint.swing2Limit.limit, swing2Axis) * twistAxis * size);
            Handles.DrawLine(position, position + Quaternion.AngleAxis(-joint.swing2Limit.limit, swing2Axis) * twistAxis * size);
        }
    }
    private GameObject ConnectToParent(Creature parent, Creature child, GameObject parentObject)
    {
        int[,] connections = parent.JointArray;
        int parentPoint = -1, childPoint = -1;

        //Gets connection details
        for (int i = 0; i < 6; i++) //Width of 2D joint array connections = 6
        {
            if (child.ID == connections[i, 0])
            {
                parentPoint = i;
                childPoint  = connections[i, 1];
            }
        }

        //Get parents connetion point transform
        Transform parentPointPos = GetPointPos(parentObject, parentPoint);
        //Get vector3 direction from parent connection point to parent center
        Vector3 directionFromParent = GetDirection(parentPointPos, parentObject.transform);

        //Move turtle to parent connection point
        turt.MoveTurtleTo(parentPointPos.position);
        //Rotate turtle to face direction from connection point to center of parent
        turt.FaceTurtleTo(directionFromParent);

        //Add child object to scene
        GameObject childObject = AddToScene(child);
        //Get distance from child object to connection point
        Transform childPointPos = GetPointPos(childObject, childPoint);
        float     childSpacing  = GetDistance(childObject.transform, childPointPos);

        //Move turtle into possition
        turt.MoveTurtle(directionFromParent.x, directionFromParent.y, directionFromParent.z, childSpacing);

        //Move child object to new turtle possition
        childObject.transform.position = turt.GetTurtlePosition();
        //Make sure child object is rotated correctly for jointing
        CorrectRotation(childObject, childPoint, parentPointPos);



        CharacterJoint charJoint  = childObject.AddComponent <CharacterJoint>();
        Vector3        localPoint = childPointPos.InverseTransformPoint(childPointPos.position);

        charJoint.anchor = localPoint;


        charJoint.connectedBody   = parentObject.GetComponent <Rigidbody>();
        charJoint.enableCollision = true;
        SoftJointLimitSpring springA = charJoint.twistLimitSpring;

        springA.damper             = 1f;
        charJoint.twistLimitSpring = springA;
        SoftJointLimitSpring springB = charJoint.swingLimitSpring;

        springB.damper                         = 1f;
        charJoint.swingLimitSpring             = springB;
        charJoint.autoConfigureConnectedAnchor = true;
        return(childObject);
    }
Exemplo n.º 7
0
        public static void ApplyDeltaToJointLimit(ref CharacterJoint joint, float delta, JointAxis jointAxis)
        {
            switch (jointAxis)
            {
            case JointAxis.Primary:
                SoftJointLimit lowX = joint.lowTwistLimit;
                lowX.limit         += delta;
                joint.lowTwistLimit = lowX;

                SoftJointLimit highX = joint.highTwistLimit;
                highX.limit         += delta;
                joint.highTwistLimit = highX;
                break;

            case JointAxis.Secondary:
                SoftJointLimit y = joint.swing1Limit;
                y.limit          += delta;
                joint.swing1Limit = y;
                break;

            case JointAxis.Tertiary:
                SoftJointLimit z = joint.swing2Limit;
                z.limit          += delta;
                joint.swing2Limit = z;
                break;
            }
        }
Exemplo n.º 8
0
	void AddJointPhysics(int n)
	{
		joints[n] = new GameObject("Joint_" + n);
		joints[n].transform.parent = transform;
		Rigidbody rigid = joints[n].AddComponent<Rigidbody>();
		SphereCollider col = joints[n].AddComponent<SphereCollider>();
		CharacterJoint ph = joints[n].AddComponent<CharacterJoint>();
		ph.swingAxis = swingAxis;
		SoftJointLimit limit_setter = ph.lowTwistLimit;
		limit_setter.limit = lowTwistLimit;
		ph.lowTwistLimit = limit_setter;
		limit_setter = ph.highTwistLimit;
		limit_setter.limit = highTwistLimit;
		ph.highTwistLimit = limit_setter;
		limit_setter = ph.swing1Limit;
		limit_setter.limit = swing1Limit;
		ph.swing1Limit = limit_setter;
		//ph.breakForce = ropeBreakForce; <--------------- TODO

		joints[n].transform.position = segmentPos[n];

		rigid.drag = ropeDrag;
		rigid.mass = ropeMass;
		col.radius = ropeColRadius;

		if (n == 1)
		{
			ph.connectedBody = transform.GetComponent<Rigidbody>();
		}
		else
		{
			ph.connectedBody = joints[n - 1].GetComponent<Rigidbody>();
		}

	}
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            if (!connectedTransforms.Contains(collision.transform))
            {
                connectedTransforms.Add(collision.transform);
                CharacterJoint j = gameObject.AddComponent <CharacterJoint>();
                j.connectedBody = collision.rigidbody;
            }
        }
        else
        {
            float mag = collision.impulse.magnitude;

            if (mag >= impulseDamageThreshold)
            {
                health -= mag * impulseDamageMultiplier;

                if (collision.gameObject.CompareTag("Player"))
                {
                    damageDealtToPlayer += mag * impulseDamageMultiplier;
                }

                if (health <= 0)
                {
                    Kill();
                }
            }
        }
    }
Exemplo n.º 10
0
        static void ConfigureJointLimits(CharacterJoint joint, float lowTwist, float highTwist, float swing1, float swing2)
        {
            if (lowTwist > highTwist)
            {
                throw new ArgumentException("wrong limitation: lowTwist > highTwist");
            }

            var twistLimitSpring = joint.twistLimitSpring;

            joint.twistLimitSpring = twistLimitSpring;

            var swingLimitSpring = joint.swingLimitSpring;

            joint.swingLimitSpring = swingLimitSpring;

            // configure limits
            var lowTwistLimit = joint.lowTwistLimit;

            lowTwistLimit.limit = lowTwist;
            joint.lowTwistLimit = lowTwistLimit;
            var highTwistLimit = joint.highTwistLimit;

            highTwistLimit.limit = highTwist;
            joint.highTwistLimit = highTwistLimit;

            var swing1Limit = joint.swing1Limit;

            swing1Limit.limit = swing1;
            joint.swing1Limit = swing1Limit;
            var swing2Limit = joint.swing2Limit;

            swing2Limit.limit = swing2;
            joint.swing2Limit = swing2Limit;
        }
Exemplo n.º 11
0
    void DetachPart(Transform part)
    {
        part.SetParent(null);
        if (!part.GetComponent <Collider>())
        {
            part.gameObject.AddComponent <BoxCollider>();
        }

        Rigidbody partRB;
        Vector3   partForce;

        partRB = part.gameObject.GetComponent <Rigidbody>();
        if (!partRB)
        {
            partRB = part.gameObject.AddComponent <Rigidbody>();
        }

        CharacterJoint joint = part.gameObject.GetComponent <CharacterJoint>();

        if (joint)
        {
            Destroy(joint);
        }

        partForce = new Vector3(Random.value - 0.5f, 1, Random.value - 0.5f);
        partRB.AddForce(partForce * 5, ForceMode.Impulse);
    }
Exemplo n.º 12
0
 private void Start()
 {
     if (_joint == null)
     {
         _joint = GetComponent <CharacterJoint>();
     }
 }
Exemplo n.º 13
0
 void Start()
 {
     armSprite            = GetComponentInChildren <SpriteRenderer>().transform;
     holdJoint            = GetComponentInChildren <CharacterJoint>();
     armBase              = new Vector3(armSprite.localScale.x, baseScale, armSprite.localScale.z);
     armSprite.localScale = armBase;
 }
Exemplo n.º 14
0
        public static CharacterJointDescription New(CharacterJoint joint, Transform root)
        {
            var d = new CharacterJointDescription();

            d.exists                 = true;
            d.connectedBody          = (joint.connectedBody != null) ? root.GetChildPath(joint.connectedBody.transform) : "";
            d.anchor                 = joint.anchor;
            d.axis                   = joint.axis;
            d.autoConfigureConnected = joint.autoConfigureConnectedAnchor;
            d.connectedAnchor        = joint.connectedAnchor;
            d.swingAxis              = joint.swingAxis;
            d.twistLimitSpring       = joint.twistLimitSpring;
            d.lowTwistLimit          = joint.lowTwistLimit;
            d.highTwistLimit         = joint.highTwistLimit;
            d.swingLimitSpring       = joint.swingLimitSpring;
            d.swing1Limit            = joint.swing1Limit;
            d.swing2Limit            = joint.swing2Limit;
            d.enableProjection       = joint.enableProjection;
            d.projectionDistance     = joint.projectionDistance;
            d.projectionAngle        = joint.projectionAngle;
            d.breakForce             = joint.breakForce;
            d.breakTorque            = joint.breakTorque;
            d.enableCollision        = joint.enableCollision;
            d.enablePreprocessing    = joint.enablePreprocessing;
            return(d);
        }
Exemplo n.º 15
0
    public void Connect(Vector3 dir)
    {
        RaycastHit hit;
        Vector3    ropeConnect;

        if (Physics.Raycast(transform.position, dir, out hit, 100f, lm, QueryTriggerInteraction.Collide))
        {
            onRope = true;
            rope.GetComponent <CharacterJoint>().anchor = Vector3.zero;

            ropeConnect             = new Vector3(hit.collider.transform.position.x, hit.collider.transform.position.y - (hit.collider.transform.localScale.y / 2), 0f);
            rope.transform.position = ropeConnect;
            rope.GetComponent <CharacterJoint>().connectedAnchor    = ropeConnect;
            rope.GetComponent <CharacterJoint>().connectedMassScale = 1;
            rope.GetComponent <CharacterJoint>().massScale          = 1;
            rope.SetActive(true);

            gameObject.AddComponent <CharacterJoint>().connectedBody = rope.GetComponent <Rigidbody>();
            CharacterJoint charJoin = GetComponent <CharacterJoint>();
            charJoin.anchor             = Vector3.zero;
            charJoin.connectedAnchor    = rope.transform.position;
            charJoin.connectedMassScale = 1;
            charJoin.massScale          = 1;

            GetComponent <Rigidbody>().velocity = GetComponent <Rigidbody>().velocity.magnitude * 1.05f * Vector3.Cross((transform.position - rope.transform.position), Vector3.back).normalized;

            lineRope.enabled = true;
            RopePosition(lineRope, transform.position, rope.transform.position);
        }
    }
Exemplo n.º 16
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JointAngularLimits"/> struct.
 /// </summary>
 /// <param name="joint">A <see cref="UnityEngine.Joint"/>.</param>
 public JointAngularLimits(Joint joint) : this()
 {
     if (joint == null)
     {
         return;
     }
     if (joint is CharacterJoint)
     {
         CharacterJoint cj = joint as CharacterJoint;
         m_XMin = cj.lowTwistLimit.limit;
         m_XMax = cj.highTwistLimit.limit;
         m_YMax = cj.swing1Limit.limit;
         m_ZMax = cj.swing2Limit.limit;
     }
     else if (joint is ConfigurableJoint)
     {
         ConfigurableJoint cj = joint as ConfigurableJoint;
         m_XMin = cj.lowAngularXLimit.limit;
         m_XMax = cj.highAngularXLimit.limit;
         m_YMax = cj.angularYLimit.limit;
         m_ZMax = cj.angularZLimit.limit;
     }
     else if (joint is HingeJoint)
     {
         HingeJoint hj = joint as HingeJoint;
         m_XMin = hj.limits.min;
         m_XMax = hj.limits.max;
     }
 }
Exemplo n.º 17
0
        public void SetRagdollParts()
        {
            RagdollParts.Clear();

            Collider[] colliders = this.gameObject.GetComponentsInChildren <Collider>();

            foreach (Collider c in colliders)
            {
                if (c.gameObject != this.gameObject)
                {
                    if (c.gameObject.GetComponent <LedgeChecker>() == null)
                    {
                        c.isTrigger = true;
                        RagdollParts.Add(c);
                        c.attachedRigidbody.interpolation          = RigidbodyInterpolation.Interpolate;
                        c.attachedRigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;

                        CharacterJoint joint = c.GetComponent <CharacterJoint>();
                        if (joint != null)
                        {
                            joint.enableProjection = true;
                        }

                        if (c.GetComponent <TriggerDetector>() == null)
                        {
                            c.gameObject.AddComponent <TriggerDetector>();
                        }
                    }
                }
            }
        }
Exemplo n.º 18
0
    // Start is called before the first frame update
    void Start()
    {
        joint = GetComponent <CharacterJoint>();
        joint.connectedBody = instantiatedController.GetComponent <Rigidbody>();

        transform.parent.transform.GetChild(2).GetComponent <RopeController>().hangingFromRope = instantiatedController.transform;
    }
Exemplo n.º 19
0
    void GenerateJoints(Transform joint)
    {
        CapsuleCollider collider       = joint.gameObject.AddComponent <CapsuleCollider>();
        CharacterJoint  characterJoint = joint.gameObject.AddComponent <CharacterJoint>();

        foreach (Transform childJoint in joint)
        {
            characterJoint.connectedBody = joint.parent.GetComponent <Rigidbody>();
            if (joint.parent == rootBone)
            {
                joint.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
            }

            joint.GetComponent <Rigidbody>().collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
            collider.center = childJoint.localPosition / 2;
            collider.height = childJoint.localPosition.magnitude;
            collider.radius = 0.0025f;

            if (childJoint.name.Contains("end"))
            {
                //joint.gameObject.AddComponent<MoveRagdoll>();
                return;
            }
            this.GenerateJoints(childJoint);
        }
    }
Exemplo n.º 20
0
        // usage of CharacterJoint
        // http://d.hatena.ne.jp/hidetobara/20111005/1317841046
        CharacterJoint CreateJoint(float limit = 120f)
        {
            joint = gameObject.AddComponent <CharacterJoint>();

            var highTwistLimit = joint.highTwistLimit;

            highTwistLimit.limit = limit;
            joint.highTwistLimit = highTwistLimit;

            var lowTwistLimit = joint.lowTwistLimit;

            lowTwistLimit.limit = -limit;
            joint.lowTwistLimit = lowTwistLimit;

            var swing1Limit = joint.swing1Limit;

            swing1Limit.limit = limit;
            joint.swing1Limit = swing1Limit;

            var swing2Limit = joint.swing2Limit;

            swing2Limit.limit = limit;
            joint.swing2Limit = swing2Limit;

            joint.enableCollision = true;
            // joint.enableCollision = false;
            joint.enablePreprocessing = false;

            return(joint);
        }
Exemplo n.º 21
0
    void TwoHandingRestricting()
    {
        Transform arm = transform.parent.parent;

        CharacterJoint handJoint = GetComponent <CharacterJoint>();
        CharacterJoint armJoint  = arm.GetComponent <CharacterJoint>();

        SoftJointLimit handSwing2limit = handJoint.swing2Limit;
        SoftJointLimit armLowLimit     = armJoint.lowTwistLimit;
        SoftJointLimit armHighLimit    = armJoint.highTwistLimit;
        SoftJointLimit armSwing1Limit  = armJoint.swing1Limit;
        SoftJointLimit handLowLimit    = handJoint.lowTwistLimit;
        SoftJointLimit handHighLimit   = handJoint.highTwistLimit;

        handSwing2limit.limit = 0;
        armLowLimit.limit     = 0;
        armHighLimit.limit    = 0;
        armSwing1Limit.limit  = 120;
        handLowLimit.limit    = 0;
        handHighLimit.limit   = 0;

        handJoint.swing2Limit    = handSwing2limit;
        armJoint.lowTwistLimit   = armLowLimit;
        armJoint.highTwistLimit  = armHighLimit;
        armJoint.swing1Limit     = armSwing1Limit;
        handJoint.lowTwistLimit  = handLowLimit;
        handJoint.highTwistLimit = handHighLimit;
    }
Exemplo n.º 22
0
    private void AddJointPhysics(int n)
    {
        //_parts[n] = new GameObject("Joint_" + n);
        _parts[n].transform.parent = transform;     // сделали созданный объект дочерним данного
        Rigidbody rigid = _parts[n].AddComponent <Rigidbody>();

        if (!useMeshCollision)
        {
            SphereCollider col = _parts[n].AddComponent <SphereCollider>();
            col.radius = ropeWidth;
        }
        CharacterJoint ph = _parts[n].AddComponent <CharacterJoint>();

        SetJointParam(ph);

        _parts[n].transform.position = _segmentPos[n];
        rigid.drag = ropeDrag;
        rigid.mass = ropeMass;

        rigid.useGravity    = gameObject.GetComponent <Rigidbody>().useGravity;
        ph.enableProjection = enableProjection; // возможно, будет восстанавливать порядок после разрушения

        if (n == 0)
        {
            ph.connectedBody = transform.GetComponent <Rigidbody>();     // зачем нулевой шарик конектится к родителю?
        }
        else
        {
            ph.connectedBody = _parts[n - 1].GetComponent <Rigidbody>();
        }
    }
Exemplo n.º 23
0
    // Use this for initialization
    void Start()
    {
        this.enemy = this.GetComponentInParent <Enemy>();
        IgnoreLocalCollisions();

        //GetComponent<Rigidbody>().isKinematic = true;
        //Destroy(this.GetComponent<CharacterJoint>());
        //Destroy(this.GetComponent<Rigidbody>());

        CharacterJoint joint = this.GetComponent <CharacterJoint>();
        Rigidbody      body  = this.GetComponent <Rigidbody>();

        if (joint != null)
        {
            jointBodyObj = joint.connectedBody.gameObject;
            Destroy(this.GetComponent <CharacterJoint>());
        }
        if (body != null)
        {
            hasRigidBody = true;
            //Destroy(this.GetComponent<Rigidbody>());
            this.GetComponent <Rigidbody>().isKinematic = true;
        }


        this.GetComponent <Collider>().isTrigger = true;
    }
Exemplo n.º 24
0
        /// <summary>
        /// Draws the controllers. Need to be invoked from 'OnSceneGUI()' method.
        /// </summary>
        /// <param name="joint">Joint.</param>
        public static void DrawControllers(Transform transform)
        {
            CharacterJoint joint = transform.GetComponent <CharacterJoint>();

            if (joint == null)
            {
                return;
            }


            Undo.RecordObject(joint, "Set Joint");

            Color   backupColor  = Handles.color;
            Vector3 position     = joint.transform.position + joint.anchor;
            float   size         = HandleUtility.GetHandleSize(position);                                       // red
            Vector3 swingAxisDir = joint.transform.TransformDirection(joint.swingAxis).normalized;              // green
            Vector3 axisDir      = joint.transform.TransformDirection(joint.axis).normalized;                   // yellow
            Vector3 direction    = GetDirection(joint, swingAxisDir, axisDir);

            DrawTwist(joint, position, direction, axisDir, size);
            DrawSwing1(joint, position, direction, axisDir, swingAxisDir, size);
            DrawSwing2(joint, position, direction, swingAxisDir, size);

            var        currRot     = Quaternion.LookRotation(swingAxisDir, axisDir);
            Quaternion newRotation = Handles.RotationHandle(currRot, position);


            joint.swingAxis = joint.transform.InverseTransformDirection(newRotation * Vector3.forward);             // green
            joint.axis      = joint.transform.InverseTransformDirection(newRotation * Vector3.up);                  // yellow



            Handles.color = backupColor;
        }
        void BuildJoints()
        {
            foreach (BoneInfo bone in bones)
            {
                if (bone.parent == null)
                {
                    continue;
                }

                CharacterJoint joint = (CharacterJoint)bone.anchor.gameObject.AddComponent <CharacterJoint>();
                bone.joint = joint;

                // Setup connection and axis
                joint.axis          = CalculateDirectionAxis(bone.anchor.InverseTransformDirection(bone.axis));
                joint.swingAxis     = CalculateDirectionAxis(bone.anchor.InverseTransformDirection(bone.normalAxis));
                joint.anchor        = Vector3.zero;
                joint.connectedBody = bone.parent.anchor.GetComponent <Rigidbody>();

                // Setup limits
                SoftJointLimit limit = new SoftJointLimit();

                limit.limit         = bone.minLimit;
                joint.lowTwistLimit = limit;

                limit.limit          = bone.maxLimit;
                joint.highTwistLimit = limit;

                limit.limit       = bone.swingLimit;
                joint.swing1Limit = limit;

                limit.limit       = 0;
                joint.swing2Limit = limit;
            }
        }
Exemplo n.º 26
0
        static void DrawTwist(CharacterJoint joint, Vector3 position, Vector3 direction, Vector3 axisDir, float size)
        {
            Handles.color = new Color(0.7f, 0.7f, 0.0f, 1f);
            Handles.ArrowHandleCap(0, position, Quaternion.LookRotation(axisDir), size * 1.1f, EventType.Repaint);

            Handles.color = new Color(0.7f, 0.7f, 0.0f, 1f);
            Vector3 twistNoraml = axisDir;
            var     hightLimit  = joint.highTwistLimit;
            var     lowLimit    = joint.lowTwistLimit;

            float newHightLimit = hightLimit.limit;
            float newLowLimit   = lowLimit.limit;

            newHightLimit = -ProcessLimit(position, twistNoraml, direction, size, -newHightLimit);
            newLowLimit   = -ProcessLimit(position, twistNoraml, direction, size, -newLowLimit);


            if (hightLimit.limit != newHightLimit)
            {
                hightLimit.limit     = newHightLimit;
                joint.highTwistLimit = hightLimit;
            }

            if (lowLimit.limit != newLowLimit)
            {
                lowLimit.limit      = newLowLimit;
                joint.lowTwistLimit = lowLimit;
            }
        }
Exemplo n.º 27
0
        protected virtual void FindMyMouth()
        {
            if (myMouth != null)
            {
                return;
            }

            // create joint
            gameObject.AddComponent <CharacterJoint>();
            myMouth = gameObject.GetComponent <CharacterJoint>();

            // configure the settings of the joint
            myMouth.autoConfigureConnectedAnchor = false;
            myMouth.anchor          = MouthPosition;
            myMouth.massScale       = 1e5f;
            myMouth.connectedAnchor = Vector3.zero;

            // set angular swing limits
            var angleLimits = new SoftJointLimit();

            angleLimits.limit      = 45f;
            myMouth.highTwistLimit = angleLimits;
            myMouth.lowTwistLimit  = angleLimits;

            // let it dangle down using gravity!
            catchableRigidbody.useGravity  = true;
            catchableRigidbody.isKinematic = false;
        }
Exemplo n.º 28
0
    private void AddJointPhysics(int n)
    {
        //_joints[n] = new GameObject("Joint_" + n);
        _joints.Insert(n, new GameObject("Joint_" + n));
        _joints[n].transform.parent = transform;
        Rigidbody rigid = _joints[n].AddComponent <Rigidbody>();

        if (!useMeshCollision)
        {
            SphereCollider col = _joints[n].AddComponent <SphereCollider>();
            col.radius = ropeWidth;
        }
        CharacterJoint ph = _joints[n].AddComponent <CharacterJoint>();

        ph.swingAxis      = swingAxis;
        ph.lowTwistLimit  = SetSoftJointLimitLimit(ph.lowTwistLimit, lowTwistLimit);
        ph.highTwistLimit = SetSoftJointLimitLimit(ph.highTwistLimit, highTwistLimit);
        ph.swing1Limit    = SetSoftJointLimitLimit(ph.swing1Limit, swing1Limit);

        //ph.breakForce = ropeBreakForce; <--------------- TODO
        _joints[n].transform.position = _segmentPos[n];
        rigid.drag = ropeDrag;
        rigid.mass = ropeMass;

        if (n == 0)
        {
            ph.connectedBody = transform.GetComponent <Rigidbody>();
        }
        else
        {
            ph.connectedBody = _joints[n - 1].GetComponent <Rigidbody>();
        }
    }
Exemplo n.º 29
0
 private void BuildJoints()
 {
     foreach (RagdollBuilder.BoneInfo boneInfo in this.bones)
     {
         if (boneInfo.parent != null)
         {
             CharacterJoint characterJoint = boneInfo.anchor.gameObject.AddComponent <CharacterJoint>();
             boneInfo.joint                     = characterJoint;
             characterJoint.axis                = RagdollBuilder.CalculateDirectionAxis(boneInfo.anchor.InverseTransformDirection(boneInfo.axis));
             characterJoint.swingAxis           = RagdollBuilder.CalculateDirectionAxis(boneInfo.anchor.InverseTransformDirection(boneInfo.normalAxis));
             characterJoint.anchor              = Vector3.zero;
             characterJoint.connectedBody       = boneInfo.parent.anchor.GetComponent <Rigidbody>();
             characterJoint.enablePreprocessing = false;
             SoftJointLimit softJointLimit = default(SoftJointLimit);
             softJointLimit.contactDistance = 0f;
             softJointLimit.limit           = boneInfo.minLimit;
             characterJoint.lowTwistLimit   = softJointLimit;
             softJointLimit.limit           = boneInfo.maxLimit;
             characterJoint.highTwistLimit  = softJointLimit;
             softJointLimit.limit           = boneInfo.swingLimit;
             characterJoint.swing1Limit     = softJointLimit;
             softJointLimit.limit           = 0f;
             characterJoint.swing2Limit     = softJointLimit;
         }
     }
 }
Exemplo n.º 30
0
    void AttachTo(GameObject other)
    {
        CharacterJoint newJoint = other.AddComponent <CharacterJoint>();

        newJoint.connectedBody = rb;
        other.GetComponent <EvolutionJoint>().ConnectWith(this.GetComponent <EvolutionJoint>());
    }