示例#1
0
    // Update is called once per frame
    void Update()
    {
        Ray myRay = new Ray(this.transform.position, this.transform.forward);

        Debug.DrawRay(myRay.origin, myRay.direction * 1000.0f);
        RaycastObject lastRaycastObject = null; ///

        RaycastHit hitObject;

        if (Physics.Raycast(myRay, out hitObject, Mathf.Infinity))
        {
            //Debug.LogFormat("Raycast hit {0}", hitObject.collider.gameObject.name);
            RaycastObject raycastHitObject = hitObject.collider.GetComponent <RaycastObject>();

            // If the hit object actually had the script, handle it
            if (raycastHitObject != null)
            {
                // If this is a NEW object, call Exit on the old object, and Enter on the new one
                if (raycastHitObject != lastRaycastObject)
                {
                    if (lastRaycastObject != null)
                    {
                        lastRaycastObject.OnRaycastExit();
                    }

                    raycastHitObject.OnRaycastEnter(hitObject);
                    lastRaycastObject = raycastHitObject;
                }
                // If this isn't a new object, just call OnRaycast on the same object
                else
                {
                    raycastHitObject.OnRaycast(hitObject);
                }
            }

            // If the object didn't have the script on it and there is a last raycast object, deactivate it
            else if (lastRaycastObject != null)
            {
                lastRaycastObject.OnRaycastExit();
                lastRaycastObject = null;
            }
        }
        // If there's no object being looked at
        else if (lastRaycastObject != null)
        {
            lastRaycastObject.OnRaycastExit();
            lastRaycastObject = null;
        }
    }
示例#2
0
    // Update is called once per frame
    void Update()
    {
        /*
         *
         * //when solving the keypad
         * if (locked && !isGazing && lastRaycastObject is KeypadObject)
         * {
         * if (!fingerMode)
         * {
         * fingerMode = true;
         * ((KeypadObject)lastRaycastObject).setPwMode(true);
         * //instantiate cursor for finger pointer
         * cursorInstance = Instantiate(Resources.Load("cursor")) as GameObject;
         * cursorInstance.SetActive(false);//cursorInstance.GetComponent<Renderer>().enabled = false;
         * }
         * if (inputPw.CompareTo("662") == 0)
         * {
         * locked = false;
         * ((KeypadObject)lastRaycastObject).setLocked(false);
         * }
         * else if (inputPw.Length > 2)
         * inputPw = "";
         * }
         *
         * if (!locked)
         * {
         * isGazing = true;
         * fingerMode = false;
         * inputPw = "";
         * inputCount = 0;
         * }
         *
         * //when the gazed object is interactive raycast object
         * if (!isGazing && lastRaycastObject is InteractiveRaycastObject)
         * {
         * //Debug.LogFormat("hi");
         * //lastRaycastObject.TurnOffMessage();
         *
         * //reset back to the gazing mode with no interaction
         * if (!((InteractiveRaycastObject)lastRaycastObject).getInInteraction())
         * {
         * isGazing = true;
         * fingerMode = false;
         * inputPw = "";
         * inputCount = 0;
         * }
         * else
         * {
         * if (fingerMode && !OVRInput.Get(OVRInput.NearTouch.SecondaryIndexTrigger))
         *  fingerRaycast();
         * return;
         * }
         * }
         *
         */


        //Debug.LogFormat("checkpoint");
        Ray myRay = new Ray(this.transform.position, this.transform.forward);

        Debug.DrawRay(transform.position, transform.forward * 1000.0f);
        RaycastHit hitObject;

        if (Physics.Raycast(myRay, out hitObject, Mathf.Infinity))
        {
            //Debug.LogFormat("checkpoint");
            RaycastObject rayCastHitObject = hitObject.collider.GetComponentInParent <RaycastObject>();
            if (rayCastHitObject != null)
            {
                if (rayCastHitObject != lastRaycastObject)
                {
                    if (lastRaycastObject != null)
                    {
                        lastRaycastObject.OnRaycastExit();
                    }
                    rayCastHitObject.OnRaycastEnter(hitObject);
                    lastRaycastObject = rayCastHitObject;
                }
                else
                {
                    rayCastHitObject.OnRayCast(hitObject);
                }
            }

            else if (lastRaycastObject != null)
            {
                Debug.LogFormat("checkpoint1");
                lastRaycastObject.OnRaycastExit();
                lastRaycastObject = null;
            }
        }
        else if (lastRaycastObject != null)
        {
            Debug.LogFormat("checkpoint2");
            lastRaycastObject.OnRaycastExit();
            lastRaycastObject = null;
        }
        //Debug.LogFormat("{0}", hitObject);
    }