Wolfos

Unity3D AI system

Apr 20th, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.71 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. /*
  5.  * AI system for Unity3D, NavMesh powered
  6.  * © Robin van Ee 2012
  7.  * Use with permission per project unless otherwise licensed
  8.  * Contact [email protected] for licensing options
  9. */
  10.  
  11. public class Enemy : MonoBehaviour {
  12.     private string[] routines = {"Idle","Patrol","Search","Attack"};
  13.     public int routelength;
  14.     public Transform[] route;
  15.     public int startroutine = 0;
  16.     //Our current routine
  17.     private int curro;
  18.     private int cur = 0;
  19.     public GameObject player;
  20.     NavMeshAgent navma;
  21.     int importance = 1;
  22.     //Estimated position of target
  23.     Vector3 estpos;
  24.     //First position we are at
  25.     Vector3 fipo;
  26.    
  27.     void Start(){
  28.         fipo = transform.position;
  29.         navma = this.GetComponent<NavMeshAgent>();
  30.         curro = startroutine;
  31.         for(int i = 0; i< routelength;i++){
  32.             route[i].gameObject.GetComponent<MeshRenderer>().enabled = false;
  33.         }
  34.     }
  35.     void Update(){
  36.         if(routines[curro]=="Idle"){
  37.             idle();
  38.         }
  39.         else if(routines[curro]=="Patrol"){
  40.             patrol();
  41.         }
  42.         else if(routines[curro]=="Search"){
  43.             search();
  44.         }
  45.         else if(routines[curro]=="Attack"){
  46.             attack();
  47.         }
  48.         hear();
  49.         see();
  50.     }
  51.    
  52.     void idle(){
  53.         navma.destination = fipo;
  54.     }
  55.    
  56.     //In this routine, we patrol a given area.
  57.     void patrol(){
  58.         importance = 2;
  59.         if(cur<routelength){
  60.             Vector3 dest = route[cur].position;
  61.             navma.destination = dest;
  62.             if(targetDistance(route[cur].position)<2){
  63.                 cur++;
  64.             }
  65.         }
  66.         else{
  67.             cur = 0;
  68.         }
  69.     }
  70.    
  71.     //In this routine, we're investigating a sound.
  72.     void search(){
  73.         importance = 2;
  74.         navma.destination = estpos;
  75.         //We're at the estimated position
  76.         if(targetDistance(estpos)<1){
  77.             //We continue where we left of before seeing or hearing the player
  78.             curro = startroutine;
  79.             //Hear and See will change estpos, so if we're hearing or seeing the player right now, we won't do this.
  80.         }
  81.     }
  82.    
  83.     void attack(){
  84.         importance = 4;
  85.         navma.destination = player.transform.position;
  86.         if(targetDistance(player.transform.position)<2){
  87.             navma.destination = transform.position;
  88.         }
  89.     }
  90.    
  91.    
  92.    
  93.     //Our senses
  94.     void hear(){
  95.         //We hear the player (he is close), but not hard enough to drop everything we're doing
  96.         //We're also not doing anything important
  97.         if(targetDistance(player.transform.position)<15 && importance<2){
  98.             //Chance of 1% (per frame) we're going to investigate
  99.             //This actually determines the accuraccy of our estimated position
  100.             if(Random.value<0.01){
  101.                 estpos = player.transform.position;
  102.                 curro = 2;
  103.             }
  104.         }
  105.         //Player is quite close, we definitely heard him now.
  106.         if(targetDistance(player.transform.position)<10 && importance<4){
  107.             //Chance of 50% (per frame) that we update our estimated position
  108.             if(Random.value<0.5){
  109.                 estpos = player.transform.position;
  110.             }
  111.             curro = 2;
  112.         }
  113.     }
  114.    
  115.     void see(){
  116.         RaycastHit hit;
  117.         if(Physics.Raycast(transform.position, Vector3.forward, out hit)){
  118.             //We saw the player
  119.             if(hit.collider.gameObject.name == player.name){
  120.                 print("saw player");
  121.                 //If close to the player, attack
  122.                 if(targetDistance(player.transform.position)<10){
  123.                     curro = 3;
  124.                 }
  125.                 else{
  126.                     //We remember where we last saw the player and investigate at that position
  127.                     estpos = player.transform.position;
  128.                     curro = 2;
  129.                 }
  130.             }
  131.         }
  132.     }
  133.    
  134.     //Simple 3D Pythagoras
  135.     float targetDistance(Vector3 target){
  136.         float distance = 0;
  137.         Vector3 distancev;
  138.         distancev.x = target.x - transform.position.x;
  139.         distancev.y = target.y - transform.position.y;
  140.         distancev.z = target.z - transform.position.z;
  141.         distance = Mathf.Sqrt(distancev.x*distancev.x + distancev.y*distancev.y);
  142.         distance = Mathf.Sqrt(distance*distance + distancev.z*distancev.z);
  143.         return distance;
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment