- Home /
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.
Have you tried OnTriggerEnter ins$$anonymous$$d?
Another alternative would be to change in trigger to false at the end of every update cycle
if i set inTrigger to false at the end of each update, the enemy slowly sinks through the ground :/ and doesnt chase you at all (with the OnTriggerEnter)
And with OnTriggerStay and the update, the same problem as before occurs.
Answer by unimechanic · Jul 11, 2014 at 03:01 PM
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.
I am using fixed update though, and the thing is returning to its position, its just got just enough time behind the player to do damage. Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613153657im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Guides or Tutorials for a Very Basic Enemy AI? [Unity 5] 1 Answer
Multiple Cars not working 1 Answer
Problems when i have multiple enemies on the same scene C# 1 Answer
Help with AI follow range 0 Answers
Emit Particle upon Death 2 Answers