Exemple #1
0
    void Update()
    {
        if (!manualControl)
        {
            // Move hand positions
            transform.localPosition = InputTracking.GetLocalPosition(handNode);
            transform.localRotation = InputTracking.GetLocalRotation(handNode) * Quaternion.Euler(60f, 0, 0);
        }

        // Triger interacts or drops item
        if (Input.GetButton(trigger) || manualTrigger)
        {
            fingers.localRotation = Quaternion.Euler(0, -145f, 0);
            thumb.localRotation   = Quaternion.Euler(85f, -30f, 0);

            if (tool != null)
            {
                if (Input.GetButtonDown(trigger))
                {
                    tool.Begin();
                }
                else
                {
                    tool.Hold();
                }
            }
        }
        else
        {
            fingers.localRotation = Quaternion.Euler(0, 0, 0);
            thumb.localRotation   = Quaternion.Euler(90f, 0, 0);

            if (tool == null)
            {
                Drop();
            }
            else
            {
                if (Input.GetButtonUp(trigger))
                {
                    tool.Stop();
                }
            }
        }

        // Send tool squeeze
        if (tool != null)
        {
            tool.Squeeze(Input.GetAxisRaw(triggerAnalog));
        }

        // Grip button drops tools
        if (Input.GetButtonDown(grip) && tool != null)
        {
            Drop();
        }

        // Grip button also functions as grab laser
        if (Input.GetButton(grip) && grabbed == null)
        {
            laser.enabled = true;
            laser.SetPosition(1, new Vector3(0, 0, 15f));

            RaycastHit hit;

            if (Physics.Raycast(transform.position, transform.forward, out hit, 1.5f, 1 << 9))
            {
                laser.SetPosition(1, new Vector3(0, 0, hit.distance * 10f));

                if (Input.GetButtonDown(trigger))
                {
                    GameObject hitGameObject = hit.transform.root.gameObject;

                    Vector3 hitOffset = hitGameObject.transform.position - hit.point;

                    Grab(hitGameObject, hitOffset);
                }
            }
        }
        else
        {
            laser.enabled = false;
        }

        // Move item positions
        if (grabbed)
        {
            grabbed.transform.position = transform.position + transform.rotation * offsetFloaty;
            grabbed.transform.rotation = transform.rotation * offsetRot;

            lastPos = transform.position;
            lastRot = transform.rotation;

            offsetFloaty = Vector3.Lerp(offsetFloaty, offsetPos, 0.1f);
            offsetSpinny = Quaternion.Lerp(offsetSpinny, offsetRot, 0.05f);
        }

        // Send trigger input to fixed update
        if (!triggered)
        {
            triggered = Input.GetButtonDown(trigger);
        }
    }