David Horák
David Horák

Reputation: 5565

Box collider without mesh, and Physics.Raycast

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

Upvotes: 3

Views: 3288

Answers (1)

Kay
Kay

Reputation: 13146

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 () {
}

Upvotes: 2

Related Questions