Skip to content

Instantly share code, notes, and snippets.

@pmarinr
Last active August 29, 2015 14:15
Show Gist options
  • Save pmarinr/032fca599309dcf5d34c to your computer and use it in GitHub Desktop.
Save pmarinr/032fca599309dcf5d34c to your computer and use it in GitHub Desktop.
Script de visión a IA
using UnityEngine;
using System.Collections;
public class vistaScript : MonoBehaviour {
RaycastHit hit;
public float angulo; // Angulo de vision de la IA
public float distancia; // Distancia max de vision
public GameObject player; // Objetivo
void Update () {
//dirección desde la IA al objetivo
Vector3 rayDirection = player.transform.position - transform.position;
// Vemos si el objetivo esta en nuestro angulo de vision
if ((Vector3.Angle(rayDirection, transform.forward)) <= angulo * 0.5f)
{
// Lanzamos un rayo para ves si podemos ver a nuestro objetivo
if (Physics.Raycast(transform.position, rayDirection, out hit, distancia))
{
Debug.DrawRay(transform.position,rayDirection,Color.green); //Dibujamos una linea verde si puede vernos
Debug.Log("Te veo");
}else{
Debug.DrawRay(transform.position,rayDirection,Color.red); //Dibujamos una linea roja en caso contrario
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment