Trigger if in view

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);
			}
		} 
	}
}

Hi!

You can try to swap the transform.position and player.transform.position in the bit where you create the ray shooting direction. You should probably also normalize the ray direction by using

 Vector3.Normalize(shootway);

Hope that helps,

~LukeWaffel

OnBecameVisible is the method will rise when the object came into view; similarly OnBecameInvisible is also there