Increase Field of vision of Enemy over time

I’m using Vector3.angle to determine when my enemy detects my player and it works fine.
However i want it so that when the player is in the Enemy’s 3rd alert stage meaning full detection the Vector3.angle increases so that the enemy can detect the player where he maybe for a period of time.

eg// full detection 22.5 and when this happens increase full detection to 180.0 for example

Heres my code :

    function React()
    {
    // This function happens when Enemy is in Full alert depending on
    // Enemy they may Fight you or engage in close combat . 
    //They may shoot at you if they are equipped If they are not Equipped they will            
    //search for the closest object they can use in their surrondings and whatever that    
    //object maybe throw it at you or shoot /aim at you
    
       Debug.Log("Get Over Here You Scum!");

       transform.position = Vector3.MoveTowards(transform.position,P_target.position,        
       patrolSpeed * Time.deltaTime);
       animation.CrossFade("Walk");
    }


    function Detect()
    {
      // This function will be used for everything related for detection or the
    
      // characteristics of the enemy when detection. Meaning there will be 3 alert
    
      // stages and each stage has its own characteristics.*
      
        var targetDir = P_target.position - transform.position;
        var forward = transform.forward;
        var angle = Vector3.Angle(targetDir, forward);
        if (angle < 90.0)
        {
           Debug.Log("Huh");
        }

        if(angle<45.0)
        {
          Debug.Log("What The");
        }

        if(angle<22.5)
        {
          Debug.Log("Oi You");
          React();
        }
    }

You should format your code using the 5th button from the left on top of the editor or highlighting all codes and press Ctrl-K. Furthermore, after you finish typing your question, before you publish it, you can scroll down to preview your question.

–

Thats strange it seems fine to me how exactly is the code displayed or formatted on your side

–

I formatted it for you this once, so it seems fine.

–

Oh alright sorry about that and thanks

–

Any ideas I'v tried a couple things but can't get it working the way i want

–

1 Answer

1

make a enum that holds your Detection State

Example

public enum DetectionState{
Invisible,
FirstStage,
SecondStage,
ThirdStage,
FullySpoted,
etc,
}

function Update(){
Detect();
switch(CurrentDetectionState){
case DetectionState.Invisible :
//Do Stuff like set angle to 25 degress
break;
case DetectionState.FirstStage:
//Do Stuff angle 60 degrees and other stuff
break;
//... and so on
}
}

you can use the same switch for your Detect Function.

I would normaly suggest something like a FinateStateMachine :stuck_out_tongue_winking_eye:

Yeah thats what I'm doing but never thought of using it for a detection state ill try that out

–

Plus i don't think it solves the problem because initially i want the enemy's field of vision to increase onces its fully detected you and once it can't see you it returns back to normal if that makes sense

–

add a float variable currentDetectionAngle and set it according to your state case DetectionState.ThirdStage : currentDetectionAngle = 180; if(angle < currentDetectionAngle) //Do your stuff

–