Question by
Davide-Barbieri · Dec 17, 2015 at 02:45 PM ·
aienemymoving
Enemy AI problem
i have a problem with enemy AI, basically i've set the Navmesh system so they can avoid obstacles and when you enter a certain distance they stop moving and they shoot, but the problem is here, if they shoot and i'm behind wall, they don't run around the wall, how can i set a method where they avoid a wall if i'm not in their view when they stop at shooting? the idea would be a continous raycast, but i've noticed a continous raycast slows down performances a lot.
here's the AI code:
private var personaggio:Transform;
var agent: NavMeshAgent;
var anim: Animator;
private var velocita:int;
var distanzaMinima:int;
var distanzaMassima: int;
private var rigid:Rigidbody;
private var velocita_salvata:int;
private var avvistato: boolean=false;
var camera_nemico: Camera;
function Start () {
InvokeRepeating("attivaRaycast",0,1);
personaggio=GameObject.FindGameObjectWithTag("Werner").transform;
velocita=Random.Range(10,15);
rigid=GetComponent.<Rigidbody>();
agent=GetComponent.<NavMeshAgent>();
anim=GetComponentInChildren.<Animator>();
}
function Update () {
if(avvistato && personaggio!=null){
CancelInvoke("attivaRaycast");
anim.SetFloat("muovi",2);
agent.Resume();
agent.SetDestination(personaggio.position);
if(Vector3.Distance(transform.position,personaggio.position) <= distanzaMinima){
anim.SetFloat("muovi",0);
anim.SetTrigger("sparo");
agent.Stop();
}
}
}
function attivaRaycast(){
var raycast_visivo:Ray[]=new Ray[Random.Range(20,20)];
var hit_visivo:RaycastHit;
for(var rv:int=0; rv<raycast_visivo.Length; rv++ ){
if(rv==0){
raycast_visivo[rv]=camera_nemico.ScreenPointToRay(Vector3(Screen.width+(rv*100), Screen.height*0.5, 0));
}else{
raycast_visivo[rv]=camera_nemico.ScreenPointToRay(Vector3(Screen.width-(rv*100), Screen.height*0.5, 0));
}
Debug.DrawRay(transform.position,raycast_visivo[rv].direction*100,Color.blue,0.5);
if(Physics.Raycast(raycast_visivo[rv],hit_visivo,50) && hit_visivo.transform.tag=="Werner"){
avvistato=true;
}
}
}
function Stop(){
yield WaitForSeconds(2);
}
Comment
Your answer
Follow this Question
Related Questions
Enemy Chasing player? 0 Answers
Guard AI is off - Please Help! 0 Answers