Esempio n. 1
0
        // ================================
        // * ACT ON LAYER PUBLIC FUNCTION *
        // ================================

        /// <summary>
        /// Interact with nearby object of given type (layer).
        /// </summary>
        /// <param name="rayFunc">The method for attempting a hit.</param>
        /// <param name="character">The character attempting the interaction.</param>
        /// <param name="layerMask">Bit mask of layers to attempt interaction with.</param>
        /// <param name="angle">Angle of cone.</param>
        /// <param name="distance">The maximum reach of the hit attempt.</param>
        /// <param name="calcScore">The function to calculate target likelihood.</param>
        /// <returns>Object interacted with, or null if none.</returns>
        static public GameObject actOnLayer(RayFunc rayFunc, GameObject character, int layerMask, float angle, float distance, ScoreFunc calcScore)
        {
            GameObject ret = null;          // game object returned by function
            float      bestScore;           // score for best candidate for target
            float      currScore;           // score for current object

            UGen.PseudoTransform charTrans; // character transform, whose position has been moved to the collider's center

            // set charTrans to character's transform information, but move position to center of character
            charTrans          = UGen.setPseudo(character.transform);
            charTrans.position = UGen.getCenter(character);

            // Get list of nearby objects in desired layers
            Collider[] objects = Physics.OverlapSphere(charTrans.position, distance, layerMask);

            // get the likeliest target object
            bestScore = float.MinValue;                       // initialize bestScore
            foreach (Collider obj in objects)                 // check each nearby object
            {
                if (rayFunc(charTrans, obj, angle, distance)) // if object was hit, check its target score
                {
                    currScore = calcScore(charTrans, obj);
                    if (currScore >= bestScore)                       // if it's likelier to be the target, set it as the target
                    {
                        bestScore = currScore;
                        ret       = obj.gameObject;
                    }
                }
            }

            return(ret);
        }
Esempio n. 2
0
 /// <summary>
 /// Interact with nearby object of given type (layer).
 /// </summary>
 /// <param name="rayFunc">The method for attempting a hit.</param>
 /// <param name="character">The character attempting the interaction.</param>
 /// <param name="layerMask">Bit mask of layers to attempt interaction with.</param>
 /// <param name="angle">Maximum angle of the hit attempt.</param>
 /// <param name="distance">The maximum reach of the hit attempt.</param>
 /// <returns>Object interacted with, or null if none.</returns>
 static public GameObject actOnLayer(RayFunc rayFunc, GameObject character, int layerMask, float angle, float distance)
 {
     return(actOnLayer(rayFunc, character, layerMask, angle, distance, aimCenterScore));
 }