Exemple #1
0
        // =======================================
        // * WEDGE LINE OF SIGHT PUBLIC FUNCTION *
        // =======================================

        /// <summary>
        /// Try to hit nearby object in a wedge in front of character. Need LOS.
        /// </summary>
        /// <param name="charTrans">Transform information of the character making the hit.</param>
        /// <param name="obj">Object of the hit attempt.</param>
        /// <param name="angle">Angle of wedge.</param>
        /// <param name="distance">The maximum reach of the hit attempt.</param>
        /// <returns>True if object is hit, else false.</returns>
        static public bool wedgeLOS(UGen.PseudoTransform charTrans, Collider obj, float angle, float distance)
        {
            RaycastHit hit;
            Vector3    centerPoint;
            Vector3    rayDirection;
            Vector2    rayXZ;     // X and Z components of rayDirection
            Vector2    forwardXZ; // X and Z components of charTrans.forward
            Vector2    rayY;      // Y component of rayDirection
            Vector2    forwardY;  // Y component of forward

            // get the closest point of the object to the character
            centerPoint = UGen.getCenter(obj.gameObject);

            // direction toward object
            rayDirection = centerPoint - charTrans.position;

            // get Vector2 equivalents to rayDirection and charTrans.forward
            rayXZ.x     = rayDirection.x;
            rayXZ.y     = rayDirection.z;
            forwardXZ.x = charTrans.forward.x;
            forwardXZ.y = charTrans.forward.z;
            rayY.x      = rayDirection.y;
            rayY.y      = 0.0f;
            forwardY.x  = charTrans.forward.y;
            forwardY.y  = 0.0f;

            // check to make sure object is not completely above or below character's collider
            if (!(UGen.getTop(obj.gameObject).y < UGen.getBottom(charTrans.gameObject).y || UGen.getBottom(obj.gameObject).y > UGen.getTop(charTrans.gameObject).y))
            {
                // check if object falls within the cone's direction
                if (Vector2.Angle(rayXZ, forwardXZ) <= angle && Vector2.Angle(rayY, forwardY) <= 90.0f)
                {
                    Debug.DrawLine(charTrans.position, centerPoint, Color.red, 5.0f, true);
                    // detect if object is within distance and LOS
                    if (Physics.Raycast(charTrans.position, rayDirection, out hit, distance, Int32.MaxValue - (int)UGen.eLayerMask.PLAYER))
                    {
                        // if it hit the object you were looking for
                        if (hit.collider.GetInstanceID() == obj.GetInstanceID())
                        {
                            return(true);
                        }
                    }
                }
            }

            // object was not hit
            return(false);
        }