- Home /
My trigger for my parent object gets triggered from child and parent,My parent object gets triggered from the child object
So I've been developing this game in which the player must shoot targets that can fire back. one of the targets is a parent of child object which is a large invisible rectangle with the soul purpose of informing the parent if it has found the player so it can attack. I also put a script in the parent that tells it that if the player successfully shoots at it without missing that it should destroy itself. But for some reason both the parent and the child can trigger this. that means with my parent can be destroyed from too far away.
in this image the blue arrow points to my parent object, the red points to the child which informs the parent if it has found the player or not and the arrow in black points at the player. (the payer can shoot up but when it hits the child object it triggers the parent to destroy itself!)
Code that kills jet upon hit: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class JetKill : MonoBehaviour {
// Use this for initialization
if (collision.CompareTag("playerShoot"))//player shoot is the tag for the players projectile
{
Destroy(gameObject);
}
}
,So I've been developing this game where the player has to shoot targets that can fire back. one of the targets is a parent object with a child paired to it that is a invisible wall meant to detect if the player is in its range. Now when I've scripted my parent object to destroy itself upon detecting the players bullets but it gets triggered even when they hit the child of the parent which I don't want
In that image the blue arrow points towards my parent object (the jet), the black one points to the player and the red one points towards the player detector. If you need anymore details ill provide as much as possible, thank you!
My parent object code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JetKill : MonoBehaviour {
public GameObject BOOM;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("jetKill")) //Kill jet at end
{
Destroy(gameObject);
}
if (collision.CompareTag("playerShoot"))
{
Instantiate(BOOM, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
Although applied on parent object it for some reason gets triggered by child
Answer by c_Feng · Dec 31, 2018 at 09:22 PM
Your colliders are being treated as a single (compound) collider. I would place each collider of the enemy in a different layer and have them only interact with the layer(s) the collider is supposed to interact with in your Physics settings.
See these other answers:
https://answers.unity.com/questions/410711/trigger-in-child-object-calls-ontriggerenter-in-pa.html
https://answers.unity.com/questions/810312/get-ontriggerenter-of-a-child.html
Answer by sean244 · Jan 01, 2019 at 08:26 PM
All you have to do is add a rigidbody to the child and set it to kinematic. Should fix your problem immediately.
Your answer
Follow this Question
Related Questions
Ontriggerstay2d only runs 26 times in a row? 3 Answers
Is There A Way To Disable a Trigger But Still Use OnTriggerExit2D? (For COOP switch management) 2 Answers
OnTriggerEnter2D called twice sometimes when IsTrigger is ON 2 Answers
How to GetComponent once for multiple comparisions 2 Answers
OnTriggerEnter2D not working 2 Answers