3
votes

I cast Raycast to only one existing Box collider in scene

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

I get message Ray hit something

But i never get trigger on the box collider

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

Target object is gameObject only with Box collider, and script for trigger checking

1
Not sure if I understand you right: Are the two objects actually colliding? Or do you expect raycasting to execute OnTriggerEnter? - Kay
@kay I want cast raycast, and if ray hit the box, then I want reacting on it in script at collider object - David Horák

1 Answers

2
votes

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

If you want to react just before the real collision, you can enlarge your collider to be for example 1.5 in size of the mesh

If you need both cases i.e. react on direct collisions and in some other situations need to take some actions before, you should split your code, e.g.:

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

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

void doSomething () {
}