- Home /
Do child object triggers trigger the parent with a separate script?
I am working on my own version of the nightmares project at the moment and encountered an odd problem.
So I want to improve upon the existing zombunny enemy with a vision filed so that he only starts running towards the player if the player is in his field of vision. I'm trying to base my solution upon the CCTV cameras from the stealth project in a way that the enemy has a TriggerCollider in front of him to determine whether the enemy can actually see the player or not.
Since the Zombunny still has another sphere collider (trigger) as an attack range attached to the parent object I wanted to attach a secondary vision field trigger as a child object to the player.
So I have the main enemy object with scripts for navigation, attacking and health attached. Also with a sphere trigger very close to the enemy so that it can attack if the player is close to it. As a sub-object there's a second "visionCone" object with the enemyVision script and another Trigger Collider attached.
If the player enters the trigger the code triggers as expected:
using UnityEngine;
using System.Collections;
public class EnemyVision : MonoBehaviour {
public Transform lookAtObject;
public float visibilityRange = 8f;
EnemyMovement enemyMove;
Ray playerVisibility;
void Start ()
{
enemyMove = GetComponentInParent<EnemyMovement>();
}
void OnTriggerStay (Collider other) {
if (other.tag == "Player")
{
enemyMove.chasePlayer = true;
}
}
}
... the enemy follows the player. So the trigger is working but the child object is apparently treated as a compound collider and also triggers the damage script on the main object.
Is there a way in which I can either prevent the viewRange Trigger from triggering the parent object scripts as well?
Or alternatively: Can I somehow restrict OnTrigger functions to a specific triggerzone on my GameObject?
Answer by Kiwasi · Nov 19, 2014 at 06:41 PM
Best way to solve is to put both triggers on seperate child objects. Put the appropriate scripts on each child.
Unity calls the various physics functions on the trigger and parent rigid body.
Thanks. That worked. I guess it is by design that child triggers are called as compound triggers, then? $$anonymous$$eaning they can't be separated out that way by code?
I believe they can be separated by code, but its more complex. Google may help here.
Answer by Immanuel-Scholz · Nov 19, 2014 at 05:03 PM
Yes, you can restrict different groups of colliders to collide with each other.
The easiest way is to put one of the gameObjects (e.g. the vision collider gameObject) into an own Layer, e.g. "VisionLayer" (you have to create that layer first under Edit/Project Settings/Tags and Layers).
Then open Edit/Project Settings/Physics (Physics2D if you want to restrict 2D collider) and uncheck the checkbox between the default layer and the new VisionLayer.
That's a good tip, thanks. Though it probably won't help in this setup, I'm afraid. If I set the trigger on the child object to not collide with the default layer then the script won't trigger the enemy chasing the player, either. I need the script to trigger as soon as the player enters the trigger zone - I just don't want the parent to be triggered by the child collider as well.
Both triggers need to be able to interact with the player but the parent script should not be triggered by the child trigger.
I see that you already got a satisfactory answer. However, if you running into difficult collision setups with unity Physics again, here is another great function to keep in your tool belt: Physics.IgnoreCollision.
It works great if you are instantiating new colliders and know a small set of gameObjects like "the one player" or "these 3 laser gates in this level" where these colliders should just pass through - trigger and collision-wise.
Cool- thanks a lot. I'll have a look at that for future enemy types ir if I'll dig into the object to replace more functions. :)
Your answer
Follow this Question
Related Questions
lighting/vision 0 Answers
How to make an objects texture what a camera is seeing 2 Answers
Enemy VISION range? Help! 0 Answers
Increase Field of vision of Enemy over time 1 Answer
OnTriggerEnter for a collider attached to another gameObject 2 Answers