Esempio n. 1
0
        // ======================================
        // * CONE LINE OF SIGHT PUBLIC FUNCTION *
        // ======================================

        /// <summary>
        /// Try to hit nearby object in a cone 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 cone.</param>
        /// <param name="distance">The maximum reach of the hit attempt.</param>
        /// <returns>True if object is hit, else false.</returns>
        static public bool coneLOS(UGen.PseudoTransform charTrans, Collider obj, float angle, float distance)
        {
            RaycastHit hit;
            Vector3    centerPoint;
            Vector3    rayDirection;

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

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

            // check if object falls within the cone's direction
            if (Vector3.Angle(rayDirection, charTrans.forward) <= angle)
            {
                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);
        }
Esempio n. 2
0
        // ======================================
        // * SCORE CALCULATION PUBLIC FUNCTIONS *
        // ======================================

        /// <summary>
        /// Calculates a score for the likelihood an object was the target based purely on angle toward target center.
        /// </summary>
        /// <remarks>Higher score means more likely.</remarks>
        /// <param name="character">Character that is targeting.</param>
        /// <param name="obj">The target.</param>
        /// <returns>The score</returns>
        static public float aimCenterScore(UGen.PseudoTransform character, Collider obj)
        {
            Vector3 objDirection; // direction from character to center of object
            float   angle;        // angle between character's forward direction and the objDirection

            objDirection = obj.bounds.center - character.position;

            angle = Vector3.Angle(character.forward, objDirection);

            return(1.0f / angle);
        }
Esempio n. 3
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);
        }