Пример #1
0
        public void Detach(bool makeJoint)
        {
            if (!detached)
            {
                detached       = true;
                tr.parent      = null;
                rb             = gameObject.AddComponent <Rigidbody>();
                rb.mass        = mass;
                rb.drag        = drag;
                rb.angularDrag = angularDrag;

                if (parentBody)
                {
                    parentBody.mass   -= mass;
                    rb.velocity        = parentBody.GetPointVelocity(tr.position);
                    rb.angularVelocity = parentBody.angularVelocity;

                    //Pick a random hinge joint to use
                    if (makeJoint && joints.Length > 0)
                    {
                        PartJoint chosenJoint = joints[Random.Range(0, joints.Length)];
                        initialAnchor   = chosenJoint.hingeAnchor;
                        displacedAnchor = initialAnchor;

                        hinge = gameObject.AddComponent <HingeJoint>();
                        hinge.autoConfigureConnectedAnchor = false;
                        hinge.connectedBody   = parentBody;
                        hinge.anchor          = chosenJoint.hingeAnchor;
                        hinge.axis            = chosenJoint.hingeAxis;
                        hinge.connectedAnchor = initialPos + chosenJoint.hingeAnchor;
                        hinge.enableCollision = false;
                        hinge.useLimits       = chosenJoint.useLimits;

                        JointLimits limits = new JointLimits();
                        limits.min        = chosenJoint.minLimit;
                        limits.max        = chosenJoint.maxLimit;
                        limits.bounciness = chosenJoint.bounciness;
                        hinge.limits      = limits;
                        hinge.useSpring   = chosenJoint.useSpring;

                        JointSpring spring = new JointSpring();
                        spring.targetPosition = chosenJoint.springTargetPosition;
                        spring.spring         = chosenJoint.springForce;
                        spring.damper         = chosenJoint.springDamper;
                        hinge.spring          = spring;
                        hinge.breakForce      = breakForce;
                        hinge.breakTorque     = breakForce;
                    }
                }
            }
        }
Пример #2
0
 /* private void OnGUI()
  * {
  *   if (GUI.Button(new Rect(10, 300, 100, 30), "OpenDoor"))
  *   {
  *       openDoor();
  *   }
  * }*/
 // Update is called once per frame
 void Update()
 {
     if (lerper < 1)
     {
         lerper += Time.deltaTime * 2;
         if (transform.GetComponent <DetachablePart>().joints[0] != null)
         {
             RVP.PartJoint temp = transform.GetComponent <DetachablePart>().joints[0];
             transform.RotateAround(transform.TransformPoint(temp.hingeAnchor), temp.hingeAxis, targetDoorAngle * Time.deltaTime * 2);
         }
         if (lerper >= 1)
         {
             if (targetDoorAngle > 0)
             {
                 open = true;
             }
             else
             {
                 open = false;
             }
         }
     }
 }