Detect when a trigger has entered a collider

I’ve got a player object that can move around. An enemy is also walking around with a large cone-shaped trigger as a kind of vision cone. The idea is that when the player enters this trigger, a script in the parent enemy object is notified so it can change its state and such.

However it seems that you can’t use an OnCollisionEnter function on the actual trigger as it won’t return anything; instead the function has to be OnTriggerEnter in the player object, so when it collides with the trigger that is the vision cone it notifies the enemy object. But if the player object is stationary when the trigger collides with it nothing happens. To work properly, the player object has to move, if only slightly, then the OnTriggerEnter function will happen. Same happens with OnTriggerStay.

Here’s the code which goes in the player object.

using UnityEngine;
using System.Collections;

public class Detection : MonoBehaviour
{
	private EnemyBehaviour enemyBehaviour;
	
	void OnTriggerStay(Collider other)
	{
		if (other.collider.tag == "VisionCone")
		{
			enemyBehaviour = other.collider.transform.root.GetComponent<EnemyBehaviour>();
			enemyBehaviour.playerSpotted = true;	
		}	
	}
}

What am I doing wrong? Or is there no way to do this at all?

  • Rigidbody attached to the gameObjects?
  • Is the Collider checked as Trigger?
  • Is one of the RidgidBodys Kinematic?
  • Use OnTriggerEnter()
  • check for other.gameObject.tag