Checking when two triggers intersect
Hi,
I'm making a stealth based element to a game I'm developing. I wish for the enemy to have a sphere collider which detects when the players movementSound sphere collider connects with it, then implement some sort of seek behaviour. Currently I have solved it with the following code in the enemy ai script:
public SphereCollider myCollider;
public PlayerMove playerObject;
void Update () {
if (myCollider.bounds.Intersects(playerObject.movementSoundSphere.bounds))
{
Debug.Log("Player Detected!!");
}
}
}
My question is, is there any better way to go about solving this, or is this current way efficient?
Many Thanks
Answer by allenallenallen · Feb 02, 2017 at 05:10 PM
Your way is inefficient. Unity has a built-in function for this situation:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
I tried to implement it this way first however I was not sure how to refer to both sphere colliders in one script
Your answer
Follow this Question
Related Questions
My AI Script doesn't make my AI turn. 1 Answer
How do I detect a collision between two objects using Bolt? 1 Answer
Check if an object is in a trigger for a certain amount of time 1 Answer
move pooled object on collision/trigger but keep pooling it 1 Answer
Trigger the Animation #2 after Animation #1 was triggered 0 Answers