Advertisement
NightFox

Unity 3D. Simply Enemy Patrol

Sep 28th, 2013
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1.  
  2. public class PatrolController : MonoBehaviour {
  3.    
  4.     // Guarda la lista de waypoints
  5.     public GameObject[] waypoint;
  6.     private int currentWaypoint;
  7.    
  8.     // Define los parametros de jugabilidad
  9.     public float speed, turn;   // turn = 0.01 y 1
  10.    
  11.     // Variables internas de control
  12.     private Vector3 move, rotation;     // Control de movimiento y rotacion
  13.     private Quaternion lookAt;          // Guarda el vector al que tienes que mirar
  14.    
  15.    
  16.    
  17.     // Use this for initialization
  18.     void Start () {
  19.        
  20.         // Inicializa las variables
  21.         currentWaypoint = 0;
  22.        
  23.         // Asigna velocidad inicial (debug)
  24.         move.z = speed;
  25.    
  26.     }
  27.    
  28.    
  29.    
  30.     // Update is called once per frame
  31.     void Update () {
  32.        
  33.         // Mueve al Zombie
  34.         MoveZombie();
  35.    
  36.     }
  37.    
  38.    
  39.    
  40.     // Mueve el enemigo por la ruta de patrulla
  41.     private void MoveZombie() {
  42.        
  43.         // Calcula el vector de a donde debe mirar el enemigo
  44.         lookAt = Quaternion.LookRotation(waypoint[currentWaypoint].transform.position - this.transform.position);
  45.        
  46.         // Apunta al target mediante una rotacion suavizada (interporlacion linear en el tiempo delta)
  47.         this.transform.rotation = Quaternion.Lerp(this.transform.rotation, lookAt, (Time.smoothDeltaTime * turn));
  48.        
  49.         // Muevelo
  50.         this.transform.Translate(move * Time.deltaTime);
  51.        
  52.     }
  53.    
  54.    
  55.    
  56.     // Si llegas al punto de patrulla, marcate como objetivo el siguiente
  57.     void OnTriggerEnter(Collider other) {
  58.        
  59.         // Si has colisionado con el Waypoint actual
  60.         if (other.gameObject == waypoint[currentWaypoint].gameObject) {
  61.             // Indica que debe dirigirse al siguiente waypoint
  62.             currentWaypoint ++;
  63.             if (currentWaypoint >= waypoint.Length) currentWaypoint = 0;
  64.         }
  65.        
  66.     }
  67.    
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement