Exemplo n.º 1
0
    //should be called OnUpdate()
    void Update()
    {
        if (objNearHand != null && joint == null && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
        {
            if (objNearHand.shouldSnap)
            {
                objNearHand.rb.position = attachPoint.position;
                objNearHand.rb.rotation = attachPoint.rotation;
            }
            //DebugConsole.dc.AddLine("Should have grabbed");
            joint = gameObject.AddComponent<FixedJoint>();
            joint.connectedBody = objNearHand.rb;
            objNearHand.OnGrab();

            light.enabled = false;
        }

        if (joint != null && device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
        {
            joint.connectedBody.isKinematic = false;
            joint.connectedBody.GetComponent<GrabbableObj>().OnDrop();

            joint.connectedBody.velocity = device.velocity;
            joint.connectedBody.angularVelocity = device.angularVelocity;

            joint.connectedBody = null;
            Object.DestroyImmediate(joint);

            light.enabled = true;
        }
        if (fireNearHand != null && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
        {
            fireNearHand.Extinguish();
        }
    }
Exemplo n.º 2
0
 protected override void OnUpdate()
 {
     if(IsPressDown() && joint == null)
     {
         if(lastCollider == null)
             return;
         
         Rigidbody targetRig = lastCollider.gameObject.GetComponent<Rigidbody>();
         
         if(targetRig != null )
         {
             MonsterController monsterController = lastCollider.transform.GetComponentInParent<MonsterController>();
             if(monsterController != null)
             {
                 joint = gameObject.AddComponent<FixedJoint>();
                 joint.connectedBody = targetRig;
                 monsterController.Grab(Vector3.zero);
             }
         }
     }
     else if (joint != null && IsPressUP())
     {
         
         MonsterController monsterController = joint.transform.root.GetComponent<MonsterController>();
         DestroyImmediate(joint);
         if(monsterController != null)
         {
             monsterController.Drop();
         }
         joint = null;
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Replaces a FixedJoint with a ConfigurableJoint.
        /// </summary>
        public static void FixedToConfigurable(FixedJoint src)
        {
            #if UNITY_EDITOR
            ConfigurableJoint conf = UnityEditor.Undo.AddComponent(src.gameObject, typeof(ConfigurableJoint)) as ConfigurableJoint;
            #else
            ConfigurableJoint conf = src.gameObject.AddComponent<ConfigurableJoint>();
            #endif

            ConvertJoint(ref conf, src as Joint);

            conf.secondaryAxis = Vector3.zero;

            conf.xMotion = ConfigurableJointMotion.Locked;
            conf.yMotion = ConfigurableJointMotion.Locked;
            conf.zMotion = ConfigurableJointMotion.Locked;

            conf.angularXMotion = ConfigurableJointMotion.Locked;
            conf.angularYMotion = ConfigurableJointMotion.Locked;
            conf.angularZMotion = ConfigurableJointMotion.Locked;

            #if UNITY_EDITOR
            UnityEditor.Undo.DestroyObjectImmediate(src);
            #else
            GameObject.DestroyImmediate(src);
            #endif
        }
Exemplo n.º 4
0
 static public int constructor(IntPtr l)
 {
     UnityEngine.FixedJoint o;
     o = new UnityEngine.FixedJoint();
     pushObject(l, o);
     return(1);
 }
Exemplo n.º 5
0
 /**
  * Connect the player to the object with a physics joint
  */
 private void AddJoint()
 {
     if (heldObj && !joint)
     {
         joint				= heldObj.AddComponent<FixedJoint> ();
         joint.connectedBody	= rigidBody;
         Debug.Log ("Joint Added to Grabbed Object", joint);
     }
 }
Exemplo n.º 6
0
 // Use this for initialization
 void Awake()
 {
     linkPool = new List<ConfigurableJoint> ();
     rigid = GetComponent<Rigidbody> ();
     playerAnchor = GetComponent<FixedJoint> ();
     controller = GetComponent<Controller> ();
     tr = transform;
     HookTarget.OnHookTargetAcquired += OnTargetAcquiredHandler;
 }
Exemplo n.º 7
0
 public void DetachFrom()
 {
     if(this.joint && this.joint.connectedBody && cartHandler != null){
         Destroy(this.joint);
         cartHandler.currentCart = null;
         cartHandler = null;
         this.joint = null;
         canBeAttached = true;
     }
 }
    private void Update()
    {
        // Make sure the user pressed the mouse down
        if (!Input.GetButton("Fire1") || isDragging)
        {
            return;
        }

        var mainCamera = FindCamera();

        // We need to actually hit an object
        RaycastHit hit = new RaycastHit();
        if (
            !Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition).origin,
                             mainCamera.ScreenPointToRay(Input.mousePosition).direction, out hit, objectDistance,
                             Physics.DefaultRaycastLayers))
        {
            return;
        }
        if (Input.GetButtonDown("Fire1"))
        {
            IInteract interactScript = hit.collider.gameObject.GetComponent<IInteract>();
            if (interactScript != null)
            {
                interactScript.interact();
                return;
            }
        }
        // We need to hit a rigidbody that is not kinematic
        if (!hit.rigidbody || hit.rigidbody.isKinematic)
        {
            return;
        }
        

        if (!m_SpringJoint)
        {
            var go = new GameObject("Rigidbody dragger");
            Rigidbody body = go.AddComponent<Rigidbody>();
            m_SpringJoint = go.AddComponent<FixedJoint>();
            body.isKinematic = true;
        }

        m_SpringJoint.transform.position = hit.point;
        m_SpringJoint.anchor = Vector3.zero;

        //m_SpringJoint.spring = k_Spring;
        //m_SpringJoint.damper = k_Damper;
        //m_SpringJoint.maxDistance = k_Distance;
        m_SpringJoint.connectedBody = hit.rigidbody;
        Rigidbody rBody = m_SpringJoint.connectedBody;
        rBody.constraints = RigidbodyConstraints.FreezeRotation;

        StartCoroutine("DragObject", hit.distance);
    }
Exemplo n.º 9
0
 public void AttachTo(ICartHandler cartHandler)
 {
     var carTransform = cartHandler.GetComponent<Transform>();
     this.transform.position = carTransform.position + carTransform.forward * 1.1f;
     this.transform.rotation = carTransform.rotation;
     this.joint = this.gameObject.AddComponent<FixedJoint>();
     this.joint.connectedBody = cartHandler.GetComponent<Rigidbody>();
     cartHandler.currentCart = this;
     canBeAttached = false;
     this.cartHandler = cartHandler;
 }
Exemplo n.º 10
0
    void attachObject()
    {
        joint = gameObject.AddComponent<FixedJoint>();
        joint.connectedBody = MiMi.GetComponent<Rigidbody>();
        joint.enablePreprocessing = false;

        joint2 = MiMi.AddComponent<FixedJoint>();
        joint2.connectedBody = gameObject.GetComponent<Rigidbody>();
        joint2.enablePreprocessing = false;
        attached = true;
    }
Exemplo n.º 11
0
    public void SetDropGameObject(GameObject dropCollectibleGameObject)
    {
        var lowerArmRight = transform.FindChild("NewDude").FindChild("Pelvis").FindChild("Chest").FindChild("UpperArmRight").FindChild("LowerArmRight");
        dropGameObject = (GameObject)Instantiate(dropCollectibleGameObject, Vector3.zero, Quaternion.Euler(0, 0, 180));
        dropGameObject.transform.parent = lowerArmRight;

        dropGameObject.transform.localPosition = Vector3.zero;
        dropGameObject.transform.localRotation = Quaternion.Euler(0, 0, 180);

        dropJoint = dropGameObject.AddComponent<FixedJoint>();
        dropJoint.connectedBody = lowerArmRight.rigidbody;
    }
Exemplo n.º 12
0
    /// <summary>
    /// Disconnects the object attached to this hand.
    /// </summary>
    /// <returns>The rigidbody of the previously connected object.</returns>
    public Rigidbody Disconnect()
    {
        currentlySelectedObject.GetComponent<GrabbableVive>().isActive = false;

        isGrabbing = false;

        //var go = joint.gameObject;
        Rigidbody rigidbody = joint.gameObject.GetComponent<Rigidbody>();
        Object.DestroyImmediate(joint);
        joint = null;
        return rigidbody;
    }
	static public int constructor(IntPtr l) {
		try {
			UnityEngine.FixedJoint o;
			o=new UnityEngine.FixedJoint();
			pushValue(l,true);
			pushValue(l,o);
			return 2;
		}
		catch(Exception e) {
			return error(l,e);
		}
	}
Exemplo n.º 14
0
    void FixedUpdate()
    {

        if (DebugMode == true)
        {
            if (ObjectGrabbed != null)
                DebugText.text = ObjectGrabbed.name;
        }

        var device = SteamVR_Controller.Input((int)trackedObj.index);
        if (joint == null && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
        {
            if (ObjectGrabbed != null)
            {
                device.TriggerHapticPulse(500);
                Rigidbody tempspace = ObjectGrabbed.GetComponent<Rigidbody>();
                MassContainer = tempspace.mass;
                tempspace.mass = 1;
                tempspace.useGravity = false;
                joint = ObjectGrabbed.AddComponent<FixedJoint>();
                joint.connectedBody = Self;
            }
        }
        else if (joint != null && device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
        {
            var go = joint.gameObject;
            var rigidbody = go.GetComponent<Rigidbody>();
            Object.DestroyImmediate(joint);
            joint = null;

            // We should probably apply the offset between trackedObj.transform.position
            // and device.transform.pos to insert into the physics sim at the correct
            // location, however, we would then want to predict ahead the visual representation
            // by the same amount we are predicting our render poses.

            var origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
            if (origin != null)
            {
                rigidbody.velocity = origin.TransformVector(device.velocity);
                rigidbody.angularVelocity = origin.TransformVector(device.angularVelocity);
            }
            else
            {
                rigidbody.velocity = device.velocity;
                rigidbody.angularVelocity = device.angularVelocity;
            }
            ObjectGrabbed.GetComponent<Rigidbody>().useGravity = true;
            ObjectGrabbed.GetComponent<Rigidbody>().mass = MassContainer;
            MassContainer = 1;
            rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;
        }
    }
Exemplo n.º 15
0
 static public int constructor(IntPtr l)
 {
     LuaDLL.lua_remove(l, 1);
     UnityEngine.FixedJoint o;
     if (matchType(l, 1))
     {
         o = new UnityEngine.FixedJoint();
         pushObject(l, o);
         return(1);
     }
     LuaDLL.luaL_error(l, "New object failed.");
     return(0);
 }
Exemplo n.º 16
0
 static public int constructor(IntPtr l)
 {
     try {
         UnityEngine.FixedJoint o;
         o = new UnityEngine.FixedJoint();
         pushValue(l, o);
         return(1);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
Exemplo n.º 17
0
 public static int constructor(IntPtr l)
 {
     try {
         UnityEngine.FixedJoint o;
         o=new UnityEngine.FixedJoint();
         pushValue(l,o);
         return 1;
     }
     catch(Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return 0;
     }
 }
Exemplo n.º 18
0
 static public int constructor(IntPtr l)
 {
     try {
         UnityEngine.FixedJoint o;
         o = new UnityEngine.FixedJoint();
         pushValue(l, true);
         pushValue(l, o);
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
Exemplo n.º 19
0
 //connect player and pickup/pushable object via a physics joint
 private void AddJoint()
 {
     if (heldObj)
     {
         if(pickUpSound)
         {
             audio.volume = 1;
             audio.clip = pickUpSound;
             audio.Play ();
         }
         joint = heldObj.AddComponent<FixedJoint>();
         joint.connectedBody = rigidbody;
     }
 }
Exemplo n.º 20
0
 //connect player and pickup/pushable object via a physics joint
 private void AddJoint()
 {
     if (heldObj)
     {
         if(pickUpSound)
         {
             GetComponent<AudioSource>().volume = 1;
             GetComponent<AudioSource>().clip = pickUpSound;
             GetComponent<AudioSource>().Play ();
         }
         joint = heldObj.AddComponent<FixedJoint>();
         joint.connectedBody = GetComponent<Rigidbody>();
     }
 }
Exemplo n.º 21
0
    void FixedUpdate()
    {
        var device = SteamVR_Controller.Input((int)trackedObj.index);
        if (joint == null && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
        {

            var go = GameObject.Instantiate(prefab);
            go.transform.position = attachPoint.transform.position;
            go.transform.rotation = attachPoint.transform.rotation;
            go.transform.Rotate(180f,180f,0f);

            joint = go.AddComponent<FixedJoint>();
            joint.connectedBody = attachPoint;
            /*prefab.transform.position = attachPoint.transform.position;
            joint = prefab.AddComponent<FixedJoint>();
            joint.connectedBody = attachPoint;
            */

        }
        else if (joint != null && device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
        {

            var go = joint.gameObject;
            var rigidbody = go.GetComponent<Rigidbody>();
            Object.DestroyImmediate(joint);
            joint = null;
            //Object.Destroy(go, 15.0f);

            // We should probably apply the offset between trackedObj.transform.position
            // and device.transform.pos to insert into the physics sim at the correct
            // location, however, we would then want to predict ahead the visual representation
            // by the same amount we are predicting our render poses.

            /*var origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
            if (origin != null)
            {
                rigidbody.velocity = origin.TransformVector(device.velocity);
                rigidbody.angularVelocity = origin.TransformVector(device.angularVelocity);
            }
            else
            {
                rigidbody.velocity = device.velocity;
                rigidbody.angularVelocity = device.angularVelocity;
            }*/

            rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;

            //prefab.transform.position = attachPoint.transform.position;
        }
    }
Exemplo n.º 22
0
 void OnCollisionStay(Collision collision)
 {
     if(collision.collider.attachedRigidbody != null && Input.GetKey(KeyCode.LeftShift) && collision.collider.attachedRigidbody.tag == "grabbable")
     {
         body2 = collision.collider.attachedRigidbody;
         flag=1;
         if(fj==null)
         {
             gameObject.AddComponent<FixedJoint>();
             fj = GetComponent<FixedJoint>();
             fj.connectedBody = body2;
         }
     }
     else if(fj!=null && !Input.GetKey(KeyCode.LeftShift))
     {
         Component.Destroy(fj);
     }
 }
Exemplo n.º 23
0
    void FixedUpdate()
    {
        var device = SteamVR_Controller.Input((int)trackedObj.index);

        //Handle info screens, only activate while holding button.
        if(device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
        {
          infoScreen.SetActive(true);
        }

        if(device.GetTouchUp(SteamVR_Controller.ButtonMask.ApplicationMenu)) {
          infoScreen.SetActive(false);
        }

        //Start grabbing. Update: No longer on GetTouchDown allowing for "sticky" hands. Though the sticky bug exists, with too slow collision detection.
        if(currentlySelectedObject != null && joint == null && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger)) {
          currentlySelectedObject.GetComponent<GrabbableVive>().isActive = true;

          isGrabbing = true;
          GameObject grabbedObject = currentlySelectedObject;
          grabbedObject.GetComponent<GrabbableVive>().onGrab();

          joint = grabbedObject.AddComponent<FixedJoint>();
          joint.connectedBody = this.gameObject.GetComponent<Rigidbody>();
        }

        //Stop grabbing.
        else if(joint != null && device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger)) {
          Rigidbody rigidbody = Disconnect();

          //Setting throw velocities?
          var origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
          if(origin != null) {
        rigidbody.velocity = origin.TransformVector(device.velocity);
        rigidbody.angularVelocity = origin.TransformVector(device.angularVelocity);
          }
          else {
        rigidbody.velocity = device.velocity;
        rigidbody.angularVelocity = device.angularVelocity;
          }

          rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;
        }
    }
Exemplo n.º 24
0
        void ConvertFixedJoint(LegacyFixed joint)
        {
            var legacyWorldFromJointA = math.mul(
                new RigidTransform(joint.transform.rotation, joint.transform.position),
                new RigidTransform(quaternion.identity, joint.anchor)
                );

            RigidTransform worldFromBodyA  = Math.DecomposeRigidBodyTransform(joint.transform.localToWorldMatrix);
            var            connectedEntity = GetPrimaryEntity(joint.connectedBody);
            RigidTransform worldFromBodyB  = connectedEntity == Entity.Null
                ? RigidTransform.identity
                : Math.DecomposeRigidBodyTransform(joint.connectedBody.transform.localToWorldMatrix);

            var bodyAFromJoint = new JointFrame(math.mul(math.inverse(worldFromBodyA), legacyWorldFromJointA));
            var bodyBFromJoint = new JointFrame(math.mul(math.inverse(worldFromBodyB), legacyWorldFromJointA));

            var jointData = JointData.CreateFixed(bodyAFromJoint, bodyBFromJoint);

            CreateJointEntity(joint.gameObject, jointData, GetPrimaryEntity(joint.gameObject), joint.connectedBody == null ? Entity.Null : connectedEntity, joint.enableCollision);
        }
        /// <summary>
        /// Write the specified value using the writer.
        /// </summary>
        /// <param name="value">Value.</param>
        /// <param name="writer">Writer.</param>
        public override void Write(object value, ISaveGameWriter writer)
        {
            UnityEngine.FixedJoint fixedJoint = (UnityEngine.FixedJoint)value;
            writer.WriteProperty("connectedBody", fixedJoint.connectedBody);
            writer.WriteProperty("axis", fixedJoint.axis);
            writer.WriteProperty("anchor", fixedJoint.anchor);
            writer.WriteProperty("connectedAnchor", fixedJoint.connectedAnchor);
            writer.WriteProperty("autoConfigureConnectedAnchor", fixedJoint.autoConfigureConnectedAnchor);
            writer.WriteProperty("breakForce", fixedJoint.breakForce);
            writer.WriteProperty("breakTorque", fixedJoint.breakTorque);
            writer.WriteProperty("enableCollision", fixedJoint.enableCollision);
            writer.WriteProperty("enablePreprocessing", fixedJoint.enablePreprocessing);
#if UNITY_2017_1_OR_NEWER
            writer.WriteProperty("massScale", fixedJoint.massScale);
            writer.WriteProperty("connectedMassScale", fixedJoint.connectedMassScale);
#endif
            writer.WriteProperty("tag", fixedJoint.tag);
            writer.WriteProperty("name", fixedJoint.name);
            writer.WriteProperty("hideFlags", fixedJoint.hideFlags);
        }
Exemplo n.º 26
0
    //What orientation and position should the 
    //object snap to the controller
    public void SnapToController(Transform _controllerPosition)
    {
        //Debug
        controllerPickingUpObject = _controllerPosition.gameObject;

        /*
        BeingHeld = true;
        //Parent the object to the controller that picked it up
        transform.parent = controllerPickingUpObject.transform;
        transform.localPosition = PositionAndOrientationToSnapTo.position;
        transform.localRotation = PositionAndOrientationToSnapTo.rotation;

        rigidBodyOfObject.useGravity = false;
        */
        Debug.Log("Trying to snap");
        BeingHeld = true;
        transform.position = AttachPoint.transform.position;
        joint = transform.gameObject.AddComponent<FixedJoint>();
        joint.connectedBody = AttachPoint;
    }
Exemplo n.º 27
0
        void ConvertFixedJoint(LegacyFixed joint)
        {
            var legacyWorldFromJointA = math.mul(
                new RigidTransform(joint.transform.rotation, joint.transform.position),
                new RigidTransform(quaternion.identity, joint.anchor)
                );

            RigidTransform worldFromBodyA  = Math.DecomposeRigidBodyTransform(joint.transform.localToWorldMatrix);
            var            connectedEntity = GetPrimaryEntity(joint.connectedBody);
            RigidTransform worldFromBodyB  = connectedEntity == Entity.Null
                ? RigidTransform.identity
                : Math.DecomposeRigidBodyTransform(joint.connectedBody.transform.localToWorldMatrix);

            var bodyAFromJoint = new BodyFrame(math.mul(math.inverse(worldFromBodyA), legacyWorldFromJointA));
            var bodyBFromJoint = new BodyFrame(math.mul(math.inverse(worldFromBodyB), legacyWorldFromJointA));

            var jointData = PhysicsJoint.CreateFixed(bodyAFromJoint, bodyBFromJoint);

            m_EndJointConversionSystem.CreateJointEntity(joint, GetConstrainedBodyPair(joint), jointData);
        }
Exemplo n.º 28
0
 void Start()
 {
     rb = GetComponent<Rigidbody>();
     joint = GetComponent<FixedJoint>();
     if (inEmergency)
     {
         hardColliders = new List<Collider>();
         Collider[] colliders = GetComponentsInChildren<Collider>();
         foreach (Collider col in colliders)
         {
             if (col.isTrigger == false)
             {
                 hardColliders.Add(col);
             }
         }
     }
     startpos = transform.position;
     startRot = transform.rotation;
     startKenematic = rb.isKinematic;
 }
Exemplo n.º 29
0
 public void AssumeCloseOrbit(IShip_Ltd ship, FixedJoint shipOrbitJoint) {
     if (_shipsInCloseOrbit == null) {
         _shipsInCloseOrbit = new List<IShip_Ltd>();
     }
     _shipsInCloseOrbit.Add(ship);
     shipOrbitJoint.connectedBody = CloseOrbitSimulator.OrbitRigidbody;
 }
Exemplo n.º 30
0
    public void AssumeHighOrbit(IShip_Ltd ship, FixedJoint shipOrbitJoint) {
        if (_shipsInHighOrbit == null) {
            _shipsInHighOrbit = new List<IShip_Ltd>();
        }
        _shipsInHighOrbit.Add(ship);

        Rigidbody highOrbitRigidbody;
        if (_emptySystemHighOrbitRigidbody != null) {
            D.Assert(!ParentSystem.Planets.Any());
            if (!_emptySystemHighOrbitRigidbody.gameObject.activeSelf) {
                _emptySystemHighOrbitRigidbody.gameObject.SetActive(true);
            }
            highOrbitRigidbody = _emptySystemHighOrbitRigidbody;
        }
        else {
            if (ParentSystem.Planets.Any()) {
                // Use the existing rigidbody used by the inner planet's celestial orbit simulator
                PlanetItem innermostPlanet = ParentSystem.Planets.MinBy(p => Vector3.SqrMagnitude(p.Position - Position)) as PlanetItem;
                highOrbitRigidbody = innermostPlanet.CelestialOrbitSimulator.OrbitRigidbody;
            }
            else {
                highOrbitRigidbody = GeneralFactory.Instance.MakeShipHighOrbitAttachPoint(gameObject);
                _emptySystemHighOrbitRigidbody = highOrbitRigidbody;
            }
        }
        shipOrbitJoint.connectedBody = highOrbitRigidbody;
    }
Exemplo n.º 31
0
        public void Drop()
        {
            if (m_held == null)
                return;

            m_held.transform.parent = null;
            m_held.isKinematic = false;

            if (m_grab != null)
            {
                if (m_basicHand.gameObject.activeSelf)
                {
                    m_basicHand.SetBool(m_grab.m_animationTransition, false);
                    m_basicHand.SetBool("Fist", true);
                }

                else if (m_AvatarAnimator != null)
                {
                    m_AvatarAnimator.SetBool(m_grab.m_animationTransition + (m_hand == Hand.RIGHT ? "Right" : "Left"), false);
                    m_AvatarAnimator.SetBool("Drop" + (m_hand == Hand.RIGHT ? "Right" : "Left"), true);
                }

                m_grab.HandControl = null;
            }
            else if (m_joint != null)
            {
                Destroy(m_joint);
            }

            if (!float.IsNaN(velocity.x) && !float.IsNaN(velocity.y) && !float.IsNaN(velocity.z))
                m_held.AddForce(velocity, ForceMode.VelocityChange);

            if (!float.IsNaN(spin.x) && !float.IsNaN(spin.y) && !float.IsNaN(spin.z))
                m_held.AddTorque(spin, ForceMode.VelocityChange);

            m_held = null;
            m_joint = null;
            m_grab = null;
            m_forced = false;
        }
Exemplo n.º 32
0
        public void Grab(Rigidbody r, GrabPoint g = null, bool forced = false)
        {
            if (r == null)
                return;

            //Debug.Log("Grab " + r.gameObject);

            m_wantsToPoint = false; // stop pointing when grabbing

            if (g == null)
            {
                m_held = r;
                m_joint = transform.gameObject.AddComponent<FixedJoint>();

                m_joint.autoConfigureConnectedAnchor = true;
                m_joint.connectedBody = m_held;

                return;
            }

            m_held = r;
            m_grab = g;
            m_grab.HandControl = this;

            if (m_AvatarAnimator != null)
            {
                m_AvatarAnimator.SetBool("Drop" + (m_hand == Hand.RIGHT ? "Right" : "Left"), false);
                m_AvatarAnimator.SetBool(m_grab.m_animationTransition + (m_hand == Hand.RIGHT ? "Right" : "Left"), true);
            }

            m_joint = transform.gameObject.AddComponent<FixedJoint>();

            m_held.isKinematic = true;

            Quaternion bindRot = Quaternion.Inverse(m_grab.GrabRotation(m_hand)) * r.transform.rotation;

            Vector3 pos = r.transform.position;
            Quaternion rot = r.transform.rotation;

            r.transform.rotation = m_sensor.rotation * bindRot;
            r.transform.position += m_sensor.position - m_grab.GrabPosition(m_hand);

            m_held.transform.parent = transform;

            targetPos = r.transform.localPosition;
            targetRot = r.transform.localRotation;

            if (!forced)
            {
                r.transform.position = pos;
                r.transform.rotation = rot;
            }
            else
            {
                m_AvatarAnimator.SetBool("Drop" + (m_hand == Hand.RIGHT ? "Right" : "Left"), false);
            }

            m_forced = forced;
        }
Exemplo n.º 33
0
	void FixedUpdate()
    {// Update cool down timer
        var device = SteamVR_Controller.Input((int)trackedObj.index);
        //if (currentCoolDownTime > 0.0f)//do scaling
        //{
            if (device.GetTouch(SteamVR_Controller.ButtonMask.Trigger))
                currentCoolDownTime -= Time.deltaTime;
            else if(currentCoolDownTime<throwCooldownTime&&!device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
                currentCoolDownTime += Time.deltaTime;
            

            if (currentCoolDownTime > 0.0f)
            {
                cooldownInstance.transform.localScale = Vector3.Lerp(Vector3.one * 0.01f, Vector3.one*0.25f, (throwCooldownTime - currentCoolDownTime) / throwCooldownTime);
            }
            else {
                 currentCoolDownTime = 0.0f;
                cooldownInstance.GetComponent<Renderer>().enabled = true;
            }
        //}

       
        if (joint == null && device.GetTouchUp (SteamVR_Controller.ButtonMask.Trigger))
        {
            
            if (currentCoolDownTime == 0.0f)
            {
                currentCoolDownTime = throwCooldownTime;
                
                /*var go = GameObject.Instantiate(prefab);
				go.transform.position = attachPoint.transform.position;

				joint = go.AddComponent<FixedJoint>();
				joint.connectedBody = attachPoint;*/

                Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
                instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
                AudioSource.PlayClipAtPoint(igniteSound, attachPoint.transform.position);

            }
        }
        // Update cool down timer
        /*if (currentCoolDownTime > 0.0f) {
			currentCoolDownTime -= Time.deltaTime;
			if (currentCoolDownTime > 0.0f) {
				cooldownInstance.GetComponent<Renderer> ().enabled = false;
			}
			else {
				currentCoolDownTime = 0.0f;
				cooldownInstance.GetComponent<Renderer> ().enabled = true;
			}
		}

		var device = SteamVR_Controller.Input((int)trackedObj.index);
		if (joint == null && device.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
		{
			if (currentCoolDownTime == 0.0f) {
				currentCoolDownTime = throwCooldownTime;

				/*var go = GameObject.Instantiate(prefab);
				go.transform.position = attachPoint.transform.position;

				joint = go.AddComponent<FixedJoint>();
				joint.connectedBody = attachPoint;

				Rigidbody instantiatedProjectile = Instantiate(projectile,transform.position,transform.rotation)as Rigidbody;
				instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0,speed));
				AudioSource.PlayClipAtPoint(igniteSound, attachPoint.transform.position);

			} 
		}*/
        else if (joint != null && device.GetTouchUp(SteamVR_Controller.ButtonMask.Trigger))
		{
			var go = joint.gameObject;
			var rigidbody = go.GetComponent<Rigidbody>();
			Object.DestroyImmediate(joint);
			joint = null;
			Object.Destroy(go, 15.0f);

			// We should probably apply the offset between trackedObj.transform.position
			// and device.transform.pos to insert into the physics sim at the correct
			// location, however, we would then want to predict ahead the visual representation
			// by the same amount we are predicting our render poses.

			var origin = trackedObj.origin ? trackedObj.origin : trackedObj.transform.parent;
			if (origin != null)
			{
				rigidbody.velocity = origin.TransformVector(device.velocity);
				rigidbody.angularVelocity = origin.TransformVector(device.angularVelocity);
			}
			else
			{
				rigidbody.velocity = device.velocity;
				rigidbody.angularVelocity = device.angularVelocity;
			}

			rigidbody.maxAngularVelocity = rigidbody.angularVelocity.magnitude;

		}

		// Position cool down indicator
		cooldownInstance.transform.position = attachPoint.transform.position;
	}
Exemplo n.º 34
0
    void Update()
    {
        if (attachedMagnet == null) {
            Vector3 thisPos = transform.position;
            foreach (Magnet magnet in closeMagnets) {
                Vector3 thatPos = magnet.transform.position;

                Vector3 thisPos1 = transform.TransformPoint(transform.localPosition + (transform.localRotation * new Vector3(0f, 0f, 0.2f)));
                Vector3 thisPos2 = transform.TransformPoint(transform.localPosition + (transform.localRotation * new Vector3(0f, 0f, -0.2f)));

                Vector3 thatPos1 = magnet.transform.TransformPoint(magnet.transform.localPosition + (magnet.transform.localRotation * new Vector3(0f, 0f, 0.2f)));
                Vector3 thatPos2 = magnet.transform.TransformPoint(magnet.transform.localPosition + (magnet.transform.localRotation * new Vector3(0f, 0f, -0.2f)));

                float dist1 = Vector3.Distance(thisPos1, thatPos2);
                float dist2 = Vector3.Distance(thisPos2, thatPos1);
                bool pos1Close = dist1 < 0.3f;
                bool pos2Close = dist2 < 0.3f;

                Debug.DrawLine(thisPos1, thatPos1, Color.green * (1f/dist1));
                Debug.DrawLine(thisPos2, thatPos2, Color.red * (1f/dist2));

                if (pos1Close && pos2Close) {
                    Debug.Log("Attaching magnet " + FullIdentifier + " to magnet " + magnet.FullIdentifier);
                    attachedMagnet = magnet;
                    attachmentJoint = collider.attachedRigidbody.gameObject.AddComponent<FixedJoint>();
                    attachmentJoint.anchor = transform.localPosition;
                    attachmentJoint.connectedBody = magnet.collider.attachedRigidbody;
                    attachmentJoint.breakForce = 10f;
                    attachmentJoint.breakTorque = 10f;

                    renderer.material = glowMaterial;
                    break;
                }
            }
        } else if (attachmentJoint == null) {
            Debug.Log("Magnet " + FullIdentifier + " no longer attached to magnet " + attachedMagnet.FullIdentifier);
            attachedMagnet = null;
            renderer.material = normalMaterial;
        }
    }
 /// <summary>
 /// Read the data using the reader.
 /// </summary>
 /// <param name="reader">Reader.</param>
 public override object Read(ISaveGameReader reader)
 {
     UnityEngine.FixedJoint fixedJoint = SaveGameType.CreateComponent <UnityEngine.FixedJoint>();
     ReadInto(fixedJoint, reader);
     return(fixedJoint);
 }
        /// <summary>
        /// Read the data into the specified value.
        /// </summary>
        /// <param name="value">Value.</param>
        /// <param name="reader">Reader.</param>
        public override void ReadInto(object value, ISaveGameReader reader)
        {
            UnityEngine.FixedJoint fixedJoint = (UnityEngine.FixedJoint)value;
            foreach (string property in reader.Properties)
            {
                switch (property)
                {
                case "connectedBody":
                    if (fixedJoint.connectedBody == null)
                    {
                        fixedJoint.connectedBody = reader.ReadProperty <UnityEngine.Rigidbody>();
                    }
                    else
                    {
                        reader.ReadIntoProperty <UnityEngine.Rigidbody>(fixedJoint.connectedBody);
                    }
                    break;

                case "axis":
                    fixedJoint.axis = reader.ReadProperty <UnityEngine.Vector3>();
                    break;

                case "anchor":
                    fixedJoint.anchor = reader.ReadProperty <UnityEngine.Vector3>();
                    break;

                case "connectedAnchor":
                    fixedJoint.connectedAnchor = reader.ReadProperty <UnityEngine.Vector3>();
                    break;

                case "autoConfigureConnectedAnchor":
                    fixedJoint.autoConfigureConnectedAnchor = reader.ReadProperty <System.Boolean>();
                    break;

                case "breakForce":
                    fixedJoint.breakForce = reader.ReadProperty <System.Single>();
                    break;

                case "breakTorque":
                    fixedJoint.breakTorque = reader.ReadProperty <System.Single>();
                    break;

                case "enableCollision":
                    fixedJoint.enableCollision = reader.ReadProperty <System.Boolean>();
                    break;

                case "enablePreprocessing":
                    fixedJoint.enablePreprocessing = reader.ReadProperty <System.Boolean>();
                    break;

                case "massScale":
#if UNITY_2017_1_OR_NEWER
                    fixedJoint.massScale = reader.ReadProperty <System.Single>();
#else
                    reader.ReadProperty <System.Single>();
#endif
                    break;

                case "connectedMassScale":
#if UNITY_2017_1_OR_NEWER
                    fixedJoint.connectedMassScale = reader.ReadProperty <System.Single>();
#else
                    reader.ReadProperty <System.Single>();
#endif
                    break;

                case "tag":
                    fixedJoint.tag = reader.ReadProperty <System.String>();
                    break;

                case "name":
                    fixedJoint.name = reader.ReadProperty <System.String>();
                    break;

                case "hideFlags":
                    fixedJoint.hideFlags = reader.ReadProperty <UnityEngine.HideFlags>();
                    break;
                }
            }
        }
Exemplo n.º 37
0
 public void OnDrop(IPanInputDevice input, FixedJoint joint)
 {
     joint.connectedBody = null;
     GameObject.DestroyImmediate(gameObject);
 }
Exemplo n.º 38
0
 public void OnPickUp(IPanInputDevice input, FixedJoint joint)
 {
     this.input = input;
     joint.connectedBody = transform.GetComponent<Rigidbody>();
  
 }
Exemplo n.º 39
0
 protected override void ConnectHighOrbitRigidbodyToShipOrbitJoint(FixedJoint shipOrbitJoint) {
     if (_highOrbitRigidbody == null) {
         _highOrbitRigidbody = GeneralFactory.Instance.MakeShipHighOrbitAttachPoint(gameObject);
     }
     shipOrbitJoint.connectedBody = _highOrbitRigidbody;
 }