private bool IsInFieldOfView(MAnimalBrain brain, Vector3 TargetCenter, Transform target)
        {
            //if (TargetCenter.y > (brain.AgentHeight + brain.transform.position.y)) return false; //Meaning the Height of the Target is higher than the animal Agent Height

            var Direction_to_Target = (TargetCenter - brain.Eyes.position);
            var Distance_to_Target  = Vector3.Distance(TargetCenter, brain.Eyes.position);

            if (Distance_to_Target < LookRange) //Check if whe are inside the Look Radius
            {
                Vector3 EyesForward = Vector3.ProjectOnPlane(brain.Eyes.forward, brain.Animal.UpVector);

                if (Vector3.Dot(Direction_to_Target.normalized, EyesForward) > Mathf.Cos(LookAngle * 0.5f * Mathf.Deg2Rad)) //Mean is in Range:
                {
                    //Need a RayCast to see if there's no obstacle in front of the Animal OBSCACLE LAYER
                    if (ObstacleLayer != 0 && Physics.Raycast(brain.Eyes.position, Direction_to_Target, out RaycastHit hit, Distance_to_Target * 0.9f, ObstacleLayer, QueryTriggerInteraction.Ignore))
                    {
                        if (brain.debug)
                        {
                            Debug.DrawLine(brain.Eyes.position, hit.point, Color.green, 0.5f);
                            Debug.DrawLine(hit.point, TargetCenter, Color.red, 0.5f);
                        }

                        if (RemoveTarget)
                        {
                            brain.RemoveTarget();
                        }
                        return(false); //Meaning there's something between the Eyes of the Animal and the Target Animal
                    }

                    if (brain.debug)
                    {
                        Debug.DrawRay(brain.Eyes.position, Direction_to_Target, Color.green, 0.5f);
                    }


                    if (brain.AIMovement.Target != target) //If the Target is different
                    {
                        if (AssignTarget)
                        {
                            brain.AIMovement.SetTarget(target, MoveToTarget);
                        }
                        //if (MoveToTarget) brain.AIMovement.SetDestination(TargetCenter);
                    }
                    return(true);
                }
            }

            if (RemoveTarget)
            {
                brain.RemoveTarget();
            }
            return(false);
        }
Beispiel #2
0
        private bool IsInFieldOfView(MAnimalBrain brain, Vector3 TargetCenter, Transform target)
        {
            var result = LookAngle <= 0 || LookRange <= 0 || ObstacleLayer == 0;

            if (result)
            {
                if (AssignTarget)
                {
                    brain.AIMovement.SetTarget(target, MoveToTarget);
                }
                else if (RemoveTarget)
                {
                    brain.RemoveTarget();
                }
                return(result);
            }


            var Direction_to_Target = (TargetCenter - brain.Eyes.position);
            var Distance_to_Target  = Vector3.Distance(TargetCenter, brain.Eyes.position);

            if (Distance_to_Target < LookRange) //Check if whe are inside the Look Radius
            {
                Vector3 EyesForward = Vector3.ProjectOnPlane(brain.Eyes.forward, brain.Animal.UpVector);

                if (Vector3.Dot(Direction_to_Target.normalized, EyesForward) > Mathf.Cos(LookAngle * 0.5f * Mathf.Deg2Rad)) //Mean is in Range:
                {
                    //Need a RayCast to see if there's no obstacle in front of the Animal OBSTACLE LAYER
                    if (Physics.Raycast(brain.Eyes.position, Direction_to_Target, out RaycastHit hit, Distance_to_Target * 0.9f, ObstacleLayer, QueryTriggerInteraction.Ignore))
                    {
                        if (brain.debug)
                        {
                            Debug.DrawLine(brain.Eyes.position, hit.point, Color.green, 0.5f);
                            Debug.DrawLine(hit.point, TargetCenter, Color.red, 0.5f);
                        }
                        result = false; //Meaning there's something between the Eyes of the Animal and the Target Animal
                    }
                    else
                    {
                        result = true;
                        if (brain.debug)
                        {
                            Debug.DrawRay(brain.Eyes.position, Direction_to_Target, Color.green, 0.5f);
                        }
                    }
                }
            }