Box collider without mesh, and Physics.Raycast

I have cast Raycast on only one existing Box collider in the scene

if (Physics.Raycast(mousePositionInWorld, transform.forward, 10))
{
   Debug.Log("Ray hit something");
}

      

I get a message, Ray hit something

But I never get a trigger on a box collider

void OnTriggerEnter(Collider other) {
        Debug.Log("Menu hit");
    }

      

Target object is gameObject with Box collider only, and script to test trigger

+3


source to share


1 answer


OnTriggerEnter (and other collider event methods) is only called if a collision actually occurs, but not by casting a ray. To solve your problem, it depends on your use case.

If you want to react just before the actual collision, you can enlarge your collider to be 1.5 mesh for example



If you need both cases, that is, react to direct collisions, and in some other situations you need to take some action earlier, you should split up your code, for example:

if (Physics.Raycast(mousePositionInWorld, transform.forward, 10)) {
   doSomething ();
}

void OnTriggerEnter(Collider other) {
   doSomething ();
}

void doSomething () {
}

      

+2


source







All Articles