- Home /
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?
Answer by OutOfRam · Oct 27, 2016 at 02:01 AM
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!
This is true for real collisions that are not triggers, right? I also have solid object that interact with the floor via gravity, but given the 2D nature of my layout I want the player and other entities to pass through these objects as if they were in the foreground or background.
Yes, it's true for both colliders and triggers.
Answer by LucianoMacaDonati · Oct 27, 2016 at 02:07 AM
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.
You gave me the same solution I'm already using. The title is the problem, not a question. I have two gameobjects with colliders that the guard moves towards then switches direction once it enters one. The problem is the collider in front of the guard being triggered on the waypoint and not the guard's main collider how I want.
Expected behaviour: Guard should switch to the next waypoint once his center collider enters the waypoint's collider.
Observed behaviour: Guard is prematurely switching to next waypoint because the child collider is triggering the script in the parent object.
I see, well, first of all a thank you wouldn't hurt, right ? Second, your initial description isn't super easy to understand so let's work together. The first problem I see with your current set up is that both colliders would trigger if they're on the same layer. Try putting the child object on a new, different layer, and make sure you uncheck the box so that it does not collide with objects on the parent's layer.