Menu
CoddyTech

El estado de alerta

Parte de la sección Arquitectura de juegos en Unity del Journey de Unity de Coddy. Lección 36 de 64.

challenge icon

Desafío

Intermedio

Alerta a la torreta cuando el jugador se acerque.

  1. En TurretStates.cs, escribe AlertState, con la cabeza y el jugador. Al entrar: cabeza Color.red, registra Intruder!. En cada actualización: apunta hacia el jugador en torno al eje y. Al salir: registra All clear.
  2. En Turret, escribe SetState(IState next): ejecuta Exit del estado anterior y después Enter del nuevo.
  3. En Update, alerta cuando esté dentro de range; en caso contrario, escanea y después actualiza el estado actual.

Presiona «Jugar» y acércate caminando. Después, pulsa «Comprobar».

Pruébalo tú mismo

using UnityEngine;

public class ScanState : IState
{
    private Transform head;
    private float angle;
    private float direction = 1f;

    public ScanState(Transform head)
    {
        this.head = head;
    }

    public void Enter()
    {
        angle = 0f;
        direction = 1f;
        head.GetComponent<Renderer>().material.color = Color.green;
    }

    public void Tick()
    {
        angle += 90f * direction * Time.deltaTime;
        if (angle > 45f)
        {
            angle = 45f;
            direction = -1f;
        }
        if (angle < -45f)
        {
            angle = -45f;
            direction = 1f;
        }
        head.localRotation = Quaternion.Euler(0f, angle, 0f);
    }

    public void Exit()
    {
    }
}

Todas las lecciones de Arquitectura de juegos en Unity

Practica por tu cuenta: Playground de Unity