Ejemplo n.º 1
0
        // returns success if any object is within distance of the current object. Otherwise it will return failure
        public override TaskStatus OnUpdate()
        {
            if (transform == null || objects == null)
            {
                return(TaskStatus.Failure);
            }

            if (overlapCast)
            {
                objects.Clear();
                if (usePhysics2D)
                {
                    if (overlap2DColliders == null)
                    {
                        overlap2DColliders = new Collider2D[maxCollisionCount];
                    }
                    var count = Physics2D.OverlapCircleNonAlloc(transform.position, magnitude.Value, overlap2DColliders, objectLayerMask.value);
                    for (int i = 0; i < count; ++i)
                    {
                        objects.Add(overlap2DColliders[i].gameObject);
                    }
                }
                else
                {
                    if (overlapColliders == null)
                    {
                        overlapColliders = new Collider[maxCollisionCount];
                    }
                    var count = Physics.OverlapSphereNonAlloc(transform.position, magnitude.Value, overlapColliders, objectLayerMask.value);
                    for (int i = 0; i < count; ++i)
                    {
                        objects.Add(overlapColliders[i].gameObject);
                    }
                }
            }

            Vector3 direction;

            // check each object. All it takes is one object to be able to return success
            for (int i = 0; i < objects.Count; ++i)
            {
                if (objects[i] == null || objects[i] == gameObject)
                {
                    continue;
                }
                direction = objects[i].transform.position - (transform.position + offset.Value);
                // check to see if the square magnitude is less than what is specified
                if (Vector3.SqrMagnitude(direction) < sqrMagnitude)
                {
                    // the magnitude is less. If lineOfSight is true do one more check
                    if (lineOfSight.Value)
                    {
                        var hitTransform = MovementUtility.LineOfSight(transform, offset.Value, objects[i], targetOffset.Value, usePhysics2D, ignoreLayerMask.value, drawDebugRay.Value);
                        if (hitTransform != null && MovementUtility.IsAncestor(hitTransform, objects[i].transform))
                        {
                            // the object has a magnitude less than the specified magnitude and is within sight. Set the object and return success
                            returnedObject.Value = objects[i];
                            return(TaskStatus.Success);
                        }
                    }
                    else
                    {
                        // the object has a magnitude less than the specified magnitude. Set the object and return success
                        returnedObject.Value = objects[i];
                        return(TaskStatus.Success);
                    }
                }
            }
            // no objects are within distance. Return failure
            return(TaskStatus.Failure);
        }