Esempio n. 1
0
 /// <summary> Set a new tracking target for this script, which also calculates new offsets </summary>
 /// <param name="newTarget"></param>
 public virtual void SetTrackingTarget(Transform newTarget, bool calculateNewOffsets)
 {
     this.trackingTarget = newTarget;
     if (trackingTarget != null && calculateNewOffsets)
     {
         SG_Util.CalculateOffsets(this.transform, trackingTarget, out this.positionOffset, out this.rotationOffset);
     }
 }
    /// <summary> Setup and/or change the tracking variables of this hand. </summary>
    /// <param name="newTarget"></param>
    /// <param name="trackType"></param>
    /// <param name="trackMethod"></param>
    /// <param name="rightHand"></param>
    protected virtual void SetupTracking(Transform newTarget, TrackingHardware trackType, TrackingMethod trackMethod, bool rightHand)
    {
        this.trackedObject    = newTarget;
        this.trackingHardware = trackType;
        this.trackingMethod   = trackMethod;

        //Calculate appropriate offsets
        if (trackedObject != null)
        {
            if (trackingHardware != TrackingHardware.Custom)
            {
                int        LR      = rightHand ? 1 : -1;
                Vector3    posOffs = Vector3.zero;
                Quaternion rotOffs = Quaternion.identity;
                if (trackingHardware == TrackingHardware.ViveTracker)
                {
                    posOffs = new Vector3(0.01f * LR, 0.04f, -0.085f);
                    rotOffs = Quaternion.Euler(0, -90, -90);
                    Debug.Log("Setting up right hand for Vive Tracker");
                }
                this.positionOffset = posOffs;
                this.rotationOffset = rotOffs;
            }
            else
            {
                SG_Util.CalculateOffsets(this.transform, this.trackedObject, out this.positionOffset, out this.rotationOffset);
            }
        }

        //Ignore collisions of the hand itself
        if (this.rigidBodyLayer != null)
        {
            if (this.physicsTrackingLayer != null)
            {
                this.rigidBodyLayer.SetIgnoreCollision(this.physicsTrackingLayer, true);
            }
            if (this.feedbackScript != null)
            {
                this.feedbackScript.SetIgnoreCollision(this.rigidBodyLayer, true);
            }
        }
        if (this.feedbackScript != null && this.physicsTrackingLayer != null)
        {
            this.feedbackScript.SetIgnoreCollision(this.physicsTrackingLayer, true);
        }

        //Apply the appropriate RB method
        if (trackingMethod == TrackingMethod.PhysicsBased && physicsTrackingLayer != null)
        {
            physicsTrackingLayer.RemoveRigidBodies();
            this.handRB     = SG_Util.TryAddRB(this.gameObject, false, false);
            ignoreGrabables = true;
            if (physicsTrackingLayer != null)
            {
                physicsTrackingLayer.gameObject.SetActive(true);
            }
        }
        else
        {
            ignoreGrabables = false;
            SG_Util.TryRemoveRB(this.gameObject); //should prevent calling OnCollisionEnter
            this.handRB = null;
            if (physicsTrackingLayer != null)
            {
                physicsTrackingLayer.gameObject.SetActive(false);
            }
        }
    }