I’m trying to make an “eye” for an enemy that it constantly facing the player and sends out a ray with Raycast, and if it hits the player it will tell the enemy to attack. I’ve gotten it to face the player and send out a ray, but I can’t get it to only react when looking at objects tagged “Player”.
I’ve tried different methods to fix this (such as, in the version of the script below, use hitInfo which causes the error “Unknown Identifier: hitInfo”) but can’t get it to work. Can anyone help me out?
var Target : Transform;
var Damping = 100.0;
var fwd = transform.TransformDirection (Vector3.forward);
function Update ()
{
lookAt();
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position,fwd))
{
//Debug.Log("I see something.");
if (hitInfo.transform.tag == "Player")
{
Debug.Log("I see the player.");
}
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * Damping);
}