Advertisement
NightFox

Unity 3D. Simply Enemy Controller

Sep 28th, 2013
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.27 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class EnemyController : MonoBehaviour {
  5.    
  6.     // Define el objetivo al que sigues
  7.     public GameObject target;
  8.    
  9.     // Define los parametros de jugabilidad
  10.     public float speed, turn;   // turn = 0.01 y 1
  11.    
  12.    
  13.     // Variables internas de control
  14.     private Vector3 move, rotation;     // Control de movimiento y rotacion
  15.     private Quaternion lookAt;          // Guarda el vector al que tienes que mirar
  16.    
  17.    
  18.    
  19.    
  20.     // Use this for initialization
  21.     void Start () {
  22.        
  23.         // Deten la ejecucion en caso de error de target
  24.         if (target == null) Debug.LogError("Target no asignado");
  25.        
  26.         // Asigna velocidad inicial (debug)
  27.         move.z = speed;
  28.    
  29.     }
  30.    
  31.    
  32.    
  33.     // Update is called once per frame
  34.     void Update () {
  35.        
  36.         // Mueve al enemigo
  37.         MoveEnemy();
  38.    
  39.     }
  40.    
  41.    
  42.    
  43.     // Mueve al enemigo
  44.     void MoveEnemy() {
  45.        
  46.         // Calcula el vector de a donde debe mirar el enemigo
  47.         lookAt = Quaternion.LookRotation(target.transform.position - this.transform.position);
  48.        
  49.         // Apunta al target mediante una rotacion suavizada (interporlacion linear en el tiempo delta)
  50.         this.transform.rotation = Quaternion.Lerp(this.transform.rotation, lookAt, (Time.smoothDeltaTime * turn));
  51.        
  52.         // Muevelo
  53.         this.transform.Translate(move * Time.deltaTime);
  54.  
  55.     }
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement