- Home /
 
Fov problem for enemy
Hello, I have detection problem for my enemy script (AIfollower.cs), a problem with fov. In every example of enemy scripts I saw that player target(transform) is defined. In my sample there are groups, e.g. group 1 (player, villagers) and group 2 (monsters, soldiers). Enemy script works as follows: it sends raycast only in front direction, if anything collides, it detects to which group belongs the game object. I want it to make detection in the line as well as 160 grade. Here is my code(AIFollower.cs):
 using UnityEngine;
 using System.Collections;
 
 public class AIFollower : MonoBehaviour {
     
     public float MaxDistance = 5f;
     private float myGroup;
     private float otherGroup;
     
     void Awake()
     {
         myGroup = transform.GetComponent<GroupIdentifer>().Group;
     }
     
     void Update () 
     {
         RaycastHit ray;
         
         Physics.Raycast(transform.position, transform.forward ,out ray,MaxDistance);
 
         if (ray.transform)
         {
             if(myGroup != ray.collider.transform.gameObject.GetComponent<GroupIdentifer>().Group)
             {
                 print("you are not in my group");
             }
             else
             {
                 print("you are my friend");
             }
         }
         else
         {
             print("I can't find anything");    
         }
         
         Debug.DrawLine(transform.position,ray.point);
     }
 }
 
 
               In script GroupIdentifer.cs there is field: public int Group;. That script is on all characters in Scene. Please help.
Your answer
 
             Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Field of view for an enemy 3 Answers
Multiple Cars not working 1 Answer
Can you name something that you spawned with a spawner script? 1 Answer
c# enemy's not spawning 1 Answer