Trigger on Child making calls to OnTrigger in Parent

I have a guard that patrols from point A and B. These points are objects with colliders that when the guard collides with it then moves in the other direction. That only if the collider on the guard itself collides with these points. I also have a child object on the guard acting as a player detector in a region in front of the guard. The player detector is making calls on the parent object to move in the other direction. Is there any way to filter which box is making calls to which object has OnTrigger?

Hello There,

The question was worded a little strangely and is a bit hard to understand, but if I do understand you correctly then what you want is to put the different trigger objects on separate layers. make a new layer and apply it to the player detector object (in much the same way you would apply a tag), then under Edit > Project Settings > Physics you will find the collision matrix (it looks like a bunch of check mark boxes. then make sure that your player detectors layer can only collider with the player or what ever it needs to collide with.

I hope this helps!

Hey man,
I’m not completely sure I understand what you’re trying to do but here’s a couple of ideas to help you out:

  • Instead of patrolling with colliders (which is very expensive for such a simple task) you could place empty game objects with labels (so that you see them). Once you’ve placed your empty objects, you could run a code like this one to patrol:

    .

     // With this, you could add more than just 2 waypoints. 
     // You could even add them dynamically !
     public List<GameObject> waypoints;
     public float speed = 1;
     int currentWaypoint = 0;
    
     void Update()
     {
         if (waypoints.Count > 1) // We only want to patrol between at least 2 points
         {
             Vector3 destination = waypoints[currentWaypoint].transform.position;
    
             // Get my "to" vector's magnitude 
             // You can make this 0.1f or something bigger so that 
             // the guard doesn't have to step "exactly" on each point
             if ((destination - transform.position).magnitude < Mathf.Epsilon)
                 // Make sure we wrap back around to start over
                 currentWaypoint = (currentWaypoint + 1) % waypoints.Count;
             else
                 // Your movement code here, ie:
                 transform.position = Vector3.Lerp(transform.position, destination, Time.deltaTime * speed);
         }
     }
    
  • Another thing that might answer your question is the ability to define physics layers. You could put the guard’s layers and player’s on specific layers so that only the ones you choose collider with other colliders. Read this for more info.

Let me know how it goes, cheers.