- Home /
Child collider triggering parent collider effects.
What I want to happen:
I want my meteor to trigger a swooshing noise when it is close to an object it can hit.
How I set it up:
I have an object (a meteor) that damages other objects (for example, a spaceship) when it collides with them.
I have attached an empty game object to the meteor and given it it's own collider (trigger) with a wider sphere. This collider does trigger the swoosh.
My problem:
The wide 'swoosher' collider on the child game object still triggers the collision effects that are supposed to be triggered only if the meteor collider hits the spaceship.
Code:
Here is the script attached to the child gameobject that is nothing more than a wide collider. My 'swoosher'. This gameobject is a child of the meteor, whose collider triggers damage (meteor code below).
public class scriptMeteorSwoosher : MonoBehaviour
{
//inspector variables
public GameObject swoosh1;
public GameObject swoosh2;
public GameObject swoosh3;
void OnTriggerEnter(Collider other)
{
if(other.gameObject.tag == "starbase")
{
//play one of the three sound effects
int rand = Random.Range(0,3);
switch(rand)
{
case 0: Instantiate(swoosh1, transform.position, transform.rotation); break;
case 1: Instantiate(swoosh2, transform.position, transform.rotation); break;
case 2: Instantiate(swoosh3, transform.position, transform.rotation); break;
}
}
}
}
Here is the code for my meteor.
public class scriptMeteor : MonoBehaviour
{
//inspector variables
public int spawnTimeMin = 20; //minimum seconds between meteor spawning
public int spawnTimeMax = 40; //maximum seconds between meteor spawning
public float speed = 0.1f;
public ParticleSystem missileHit;
public ParticleSystem baseHit;
//private variables
private float spawnTimer; //for recording current spawn time
private Vector3 direction;
// Use this for initialization
void Start ()
{
//generate an initial value for the spawntimer
spawnTimer = Random.Range (spawnTimeMin,spawnTimeMax);
}
// Update is called once per frame
void Update ()
{
MeteorSpawner();
UpdateMeteor();
}
//function for moving the meteor to it's new location
void MeteorSpawner()
{
//declare required variables
float randx;
float randy;
float randDirX;
float randDirY;
Vector3 tempPos = new Vector3(0,0,0);
//reduce the spawntimer by one per second
spawnTimer = spawnTimer -(1 * Time.deltaTime);
//when it is time to spawn the meteor again
if(spawnTimer <= 0)
{
//generate new random x/y coords where maximums are outside viewable game window
randx = Random.Range (-17.0f,17.0f);
randy = Random.Range (-10.0f,10.0f);
//make sure it spawns outside the viewable game are but not too far outside
//width
while((randx > -16.5) && (randx < 16.5))
{
randx = Random.Range (-17.0f,17.0f);
}
//height
while((randy > -9.5) && (randy < 9.5))
{
randy = Random.Range (-10.0f,10.0f);
}
//if it's above y zero, point it down
if(randy > 0)
{
randDirY = Random.Range (-180.0f,-1.0f);
}
//else point it up
else
{
randDirY = Random.Range (1.0f,180.0f);
}
//if it's left of x zero, point it right
if(randx > 0)
{
randDirX = Random.Range (-180.0f,-1.0f);
}
//else point it left
else
{
randDirX = Random.Range (1.0f,180.0f);
}
//combine the direction for the meteor to go into a new vector3 and normalize it
direction = new Vector3(randDirX,randDirY,0);
direction.Normalize();
//assign tempPos the new vector3 and move the meteor there
tempPos = new Vector3(randx,randy,2);
transform.position = tempPos;
//reset the spawntimer
spawnTimer = Random.Range (spawnTimeMin,spawnTimeMax);
}
}
void UpdateMeteor()
{
//just move the meteor forward. that is all
transform.position += direction * speed;
}
//if it collides with anything it needs to do something about
void OnTriggerEnter(Collider other)
{
//if it hits a missile pretend it's destroyed by moving it out of the viewable area
if(other.gameObject.tag == "missile")
{
Instantiate(missileHit, transform.position, transform.rotation);
Vector3 awayWithThee = new Vector3(0,0,500);
transform.position = awayWithThee;
}
//if it hits a starbase, change it's Z position so it will not trigger any more collisions until it is moved again
if(other.gameObject.tag == "starbase")
{
Instantiate(baseHit, transform.position, transform.rotation);
transform.position = new Vector3(transform.position.x, transform.position.y, 5.9f);
}
}
}
Answer by GregXavier · May 02, 2013 at 11:42 PM
Derp. Found the answer here... Evidently my initial google was not sufficient!
http://answers.unity3d.com/questions/410711/trigger-in-child-object-calls-ontriggerenter-in-pa.html
Works flawlessly when I give the swoosher collider a kinematic rigidbody.
Your answer
Follow this Question
Related Questions
Problems with the triggers(not recognized) 1 Answer
Trigger and Collider? 3 Answers
C# Make two objects collide to activate a third object 3 Answers
Return gameobject from a TriggerEvent. 1 Answer
Get object that triggered an object 1 Answer