Weeping Angel AI / OnTriggerStay Problems

Hi,

I have a C# script with which i am trying to create a simple weeping angel style AI. I am using OnTriggerStay to check wether or not he is in his area (defined by a trigger named WeepingQuadrant). However I am having trouble keeping him in his quadrant while the player is not in the quadrant, as currently he quickly teleports in and out of the quadrant. However he is behind the player long enough for the damage script to damage the player, even though he is not supposed to be doing so.

Below is my code:


using UnityEngine;
using System.Collections;

public class WeepingAI : MonoBehaviour
{

		public Transform player;
		MeshRenderer objectRenderer;
		Vector3 originalPos;
		public bool inTrigger = false;
		public float dotProduct;
		bool canHurt = false;
		
		// Use this for initialization
		void Start ()
		{
				originalPos = transform.position;
				objectRenderer = GetComponentInChildren<MeshRenderer> ();
		}
	
	
		void FixedUpdate ()
		{
				if (!inTrigger) {
						transform.position = originalPos;
						canHurt = false;
						gameObject.GetComponent<MeshRenderer> ().enabled = false;
				}
				if (inTrigger) {
						if (CheckIfOffScreen ()) {
								//All move code here;
								transform.LookAt (new Vector3 (player.position.x, transform.position.y, player.position.z));
								if (Vector3.Distance (player.position, transform.position) > 5) {
										transform.position = player.position - player.forward;
								}
								if (Vector3.Distance (player.position, transform.position) < 1) {
										player.GetComponent<Player> ().health -= 0.01f;
										player.GetComponent<Player> ().HurtSound ();
								}
						}
				}
		}
		
		
		//If hes in his quadrant
		IEnumerator OnTriggerStay (Collider col)
		{
				yield return new WaitForFixedUpdate ();
				if (col.CompareTag ("WeepingQuadrant")) {
						//Check if the player can see it
						inTrigger = true;

				}
		}
		void OnTriggerExit (Collider col)
		{
				if (col.CompareTag ("WeepingQuadrant"))
						inTrigger = false;

		}
		
		bool CheckIfOffScreen ()
		{
				Vector3 playerFwd = player.forward;
				Vector3 targetDir = (player.position - transform.position).normalized;
				dotProduct = Vector3.Dot (playerFwd, targetDir); 
				if (dotProduct < -0.5) {
						return false;
				} else {
						return true;
				}
				
		}
}

Any help would be greatly appreciated.

Thanks,

IEMatrixGuy out.

If you want physics to work correctly manipulate objects with Rigidbody’s members and do it in FixedUpdate. I see you are using Transform.

Physics Tips:

If a collider is loaded/unloaded along with the scene and is not altered in any way, it can be a static collider (without Rigidbody).

If a collider is created/destroyed dynamically or is modified in some way, it must be a dynamic collider (needs a Rigidbody).

Altering means enabling (SetActive) or transforming (moving, rotating, scaling).

Whenever there is a GameObject with a Rigidbody, it must always be transformed with the corresponding Rigidbody’s members, always in FixedUpdate.