Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PatrolController : MonoBehaviour {
- // Guarda la lista de waypoints
- public GameObject[] waypoint;
- private int currentWaypoint;
- // Define los parametros de jugabilidad
- public float speed, turn; // turn = 0.01 y 1
- // Variables internas de control
- private Vector3 move, rotation; // Control de movimiento y rotacion
- private Quaternion lookAt; // Guarda el vector al que tienes que mirar
- // Use this for initialization
- void Start () {
- // Inicializa las variables
- currentWaypoint = 0;
- // Asigna velocidad inicial (debug)
- move.z = speed;
- }
- // Update is called once per frame
- void Update () {
- // Mueve al Zombie
- MoveZombie();
- }
- // Mueve el enemigo por la ruta de patrulla
- private void MoveZombie() {
- // Calcula el vector de a donde debe mirar el enemigo
- lookAt = Quaternion.LookRotation(waypoint[currentWaypoint].transform.position - this.transform.position);
- // Apunta al target mediante una rotacion suavizada (interporlacion linear en el tiempo delta)
- this.transform.rotation = Quaternion.Lerp(this.transform.rotation, lookAt, (Time.smoothDeltaTime * turn));
- // Muevelo
- this.transform.Translate(move * Time.deltaTime);
- }
- // Si llegas al punto de patrulla, marcate como objetivo el siguiente
- void OnTriggerEnter(Collider other) {
- // Si has colisionado con el Waypoint actual
- if (other.gameObject == waypoint[currentWaypoint].gameObject) {
- // Indica que debe dirigirse al siguiente waypoint
- currentWaypoint ++;
- if (currentWaypoint >= waypoint.Length) currentWaypoint = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement