I’m trying to make an enemy have a weeping angel component, but the code doesn’t seem to work. I’m using a raycast to see if they’re in view, but the raycast doesn’t go to the right spot and the enemy doesn’t move. Can someone see what’s wrong or figure out a better idea?
using UnityEngine;
using System.Collections;
using UnityEngine.AI;
public class EnemyFollow : MonoBehaviour
{
Transform player; // Reference to the player's position.
NavMeshAgent nav; // Reference to the nav mesh agent.
void Awake ()
{
// Set up the references.
player = GameObject.FindGameObjectWithTag ("Player").transform;
nav = GetComponent <NavMeshAgent> ();
}
void Update ()
{
Vector3 shootway = transform.position - player.transform.position;
RaycastHit rayHit;
Ray rayTest = new Ray(transform.position, shootway);
Debug.DrawRay(transform.position, player.transform.position, Color.blue);
if (Physics.Raycast(rayTest,out rayHit))
{
if (rayHit.collider.gameObject.tag == "Player")
{
nav.SetDestination (player.position);
}
}
}
}