Пример #1
0
    /**
     * Date:             May 3, 2017
     * Author:           Jay Coughlan
     * Interface:        void Update ()
     * Description:
     *                   Update is called once per frame.
     *
     * Revision:			Aing Ragunathan (May 17, 2017) - Added call to playerMovement.cs to update speed when consumable is obtained
     */
    void Update()
    {
        //instead of finding this objects position all the time, we simply put it into a static variable and call it once a frame.
        thisPosition = this.transform.position;
        thisForward  = this.transform.forward;

        //we update our raycast
        //set the ray to start at our position
        ray.origin = thisPosition;
        //set it's direction
        ray.direction = thisForward;

        //we are going to clamp the values of the layers now to make sure they fit appropriately within each other
        min    = Mathf.Clamp(min, 0, layer2 - 0.01f);
        layer2 = Mathf.Clamp(layer2, min + 0.01f, layer3 - 0.01f);
        layer3 = Mathf.Clamp(layer3, layer2 + 0.01f, max - 0.01f);
        max    = Mathf.Max(layer2 + 0.01f, max);

        //now we need to make sure we're testing the proper headOnRange
        //literalHeadOnRange = 180 - headOnRange;
        if (debug)
        {
            Debug.DrawRay(ray.origin, ray.direction, Color.green);
        }

        if (Physics.Raycast(ray, out hit, Vector3.Magnitude(ray.direction)))
        {
            switch (hit.collider.tag)
            {
            case "Entrance":
            //so far entrance behavior is the same as obstacle, so we flow through
            case "Walls":
            //so far wall behavior is the same as obstacle so we flow through
            case "Obstacle":
                //show off our fancy raycast skillz and turn it red.
                if (debug)
                {
                    Debug.LogWarning("The raycast has hit " + hit.collider.name);
                    Debug.DrawRay(ray.origin, ray.direction, Color.red);
                }
                //if the raycast has hit (and if we're here, it has)
                //you ded son

                pmScript.lowerHealth(pmScript.getHealth(), true);
                break;

            case "Start Volume":
                pmScript.startTimer();
                break;

            case "End Volume":
                pmScript.endTimer();
                break;

            default:
                //if we hit anything that's not walls, the entrance, or an obstacle, turn it yellow, but continue.
                if (debug)
                {
                    Debug.LogWarning("The raycast has hit " + hit.collider.name);
                    Debug.DrawRay(ray.origin, ray.direction, Color.yellow);
                }
                break;
            }
        }

        //**************************************************************************//
        //                     This is Obstacles                                    //
        //for each object we're currently colliding with, we want to check how far away the object is.
        //Depending on what threshold it falls into, we want to do specific defined behaviour
        for (int index = 0; index < collidedObjects.Count; index++)
        {
            //first we grab the distance between us and the colliding object
            distance = Vector3.Distance(thisPosition, collisions[index]);//.transform.position);

            //this is the minimum distance, and will be used to determine the result on OnTriggerExit()
            if (distance < collidedMinDistances[index])
            {
                collidedMinDistances[index] = distance;
            }

            //we catch the distance between layers and act accordingly
            //with the exception of death, you likely won't want to put much code in this logic, because it will ocurr on every update
            //for most logic you want to put it into the "OnTriggerExit()" method, so it occurs once.
            //if the object is within the minimum bounds.
            if (distance < min)
            {
                //likely death state will be acitvated here
                if (debug)
                {
                    Debug.DrawLine(thisPosition, collisions[index], colors[0]);
                }
                //here we will start with damaging code
                pmScript.lowerHealth(1);
            }
            //if the object is within layer 2

            else if (distance < layer2)
            {
                //probably max pointage
                if (debug)
                {
                    Debug.DrawLine(thisPosition, collisions[index], colors[1]);
                }
            }
            //if the object is within layer 3
            else if (distance < layer3)
            {
                //minor pointage goes here

                if (debug)
                {
                    Debug.DrawLine(thisPosition, collisions[index], colors[2]);
                }
            }
            //if the object is within the maximum bounds
            else if (distance < max)
            {
                //likely nothing happens here
                if (debug)
                {
                    Debug.DrawLine(thisPosition, collisions[index], colors[3]);
                }
            }
            else
            {
                Debug.DrawLine(thisPosition, collisions[index], Color.cyan);
            }
        }

        //**************************************************************************//
        //                   This is for consumables                                //
        //for each consumable we're currently colliding with, we want to check how far away the object is.
        //Depending on what threshold it falls into, we want to do specific defined behaviour
        for (int index = 0; index < consumedObjects.Count && index >= 0; index++)
        {
            //first we grab the distance between us and the colliding object
            distance = Vector3.Distance(thisPosition, consumedCollisions[index]);//.transform.position);
            consumedDistances[index] = distance;

            //this is the minimum distance, and will be used to determine the result on OnTriggerExit()

            /* if (distance < collidedMinDistances[index])
             * {
             *  collidedMinDistances[index] = distance;
             * }*/

            //we catch the distance between layers and act accordingly
            //with the exception of death, you likely won't want to put much code in this logic, because it will ocurr on every update
            //for most logic you want to put it into the "OnTriggerExit()" method, so it occurs once.
            //if the object is within the minimum bounds.
            if (distance < min)
            {
                //likely death state will be acitvated here
                if (debug)
                {
                    Debug.DrawLine(thisPosition, consumedCollisions[index], colors[0]);
                }

                //here we will start with damaging code
                //pmScript.lowerHealth(1);

                //change player's properties depneding on the consumable object
                pmScript.consume(consumedObjects[index]);    //increase speed of player and disable it
                consumedCollisions.RemoveAt(index);
                consumedAngles.RemoveAt(index);
                consumedDistances.RemoveAt(index);
                consumedObjects.RemoveAt(index);
                index--;
            }
            //if the object is within layer 2
            else if (distance < layer2)
            {
                //probably max pointage
                if (debug)
                {
                    Debug.DrawLine(thisPosition, consumedCollisions[index], colors[1]);
                }
            }
            //if the object is within layer 3
            else if (distance < layer3)
            {
                //minor pointage goes here

                if (debug)
                {
                    Debug.DrawLine(thisPosition, consumedCollisions[index], colors[2]);
                }
            }
            //if the object is within the maximum bounds
            else if (distance < max)
            {
                //likely nothing happens here
                if (debug)
                {
                    Debug.DrawLine(thisPosition, consumedCollisions[index], colors[3]);
                }
            }
            else
            {
                Debug.DrawLine(thisPosition, consumedCollisions[index], Color.cyan);
            }
        }


        //we turn off the collider rendering during gameplay if we're not debugging
        //we run this code again in update in case the user wants to check debugging mid run time
        //we check if maxRend is enabled, if it is we assume they're all enabled. if it's not we assume everything else is disabled as well.
        //no point in running disabling code on disabled functions
        if (!debug && maxRend.enabled == true)
        {
            maxRend.enabled  = false;
            lay3Rend.enabled = false;
            lay2Rend.enabled = false;
            minRend.enabled  = false;
        }
        else if (debug && maxRend.enabled == false)//Same as above, but in reverse.
        {
            maxRend.enabled  = true;
            lay3Rend.enabled = true;
            lay2Rend.enabled = true;
            minRend.enabled  = true;
        }

        //we also want to make sure the size of the collider is the same as our max size, otherwise there's not much point in having a max size
        maximumSphere.transform.localScale = new Vector3(max * 2, max * 2, max * 2);
        layer2Sphere.transform.localScale  = new Vector3(layer2 * 2, layer2 * 2, layer2 * 2);
        layer3Sphere.transform.localScale  = new Vector3(layer3 * 2, layer3 * 2, layer3 * 2);
        minimumSphere.transform.localScale = new Vector3(min * 2, min * 2, min * 2);
    }