- Home /
How do you trigger a specific collider using OnTriggerEnter?
Hey, guys, I'm new to unity, and C# in general, but I am wanting to have a prefab, which has 2 colliders; use one for general detection of triggers, and one specifically for the use of another prefab's OnTriggerEnter. Any help is welcome, and I hope to hear from you soon!
void OnTriggerEnter(Collider particles)
{
Flight();
//Destroy(gameObject);
}
I know its old, but for those who wondered in from Google:
public void OnCollisionEnter(Collision c)
{
//this is the rigidbody that was collided. This will be a parent object if that is where the rigidbody is assigned.
Debug.Log("Collided with: " + c.transform.name);
//this is the collider that was collided. It will be the transform which contains the collider and will NOT be a parent
Debug.Log("Specifically, the part collided with was " + c.collider.transform.name);
}
So a parent should have a rigidbody. It can have 1 collider on it. Then add a child transform and add a collider to the child. There can be multiple children, each with their own collider.
The children are automatically part of the rigidbody in their parent.
Collision.transform will refer to the parent which has a rigidbody. Collision.collider.transform will refer to the child which has a collider.
Answer by a161803398874 · Jul 29, 2017 at 07:24 AM
@MS77Lieger Hi , you should use the onCollisionEnter method, you can use several properties to make shure you can trigger the exact collider you want, for this method to work you need to set correctly the properties of the object you want to collide with... first you need to make shure that the check mark in the Collider Properties (inspector window) that says "IsTrigger" is checked ,this is so the trigger method can detect it...When this is checked objects can go thru this collider not hiting with him, so you may need to put two colliders on the object makin the "IsTrigger" one bigger than the normal one so the system can detect the collider... Next you need to decide on how you are going to detect the collision, this can be with "GameTag" "Name" or others... @Goldiedog123 gave an example using "Gametag" we will use the "Name" of the game object.
void OnCollisionEnter(Collision dataFromCollision)
{
if (dataFromCollision.gameObject.name == "GameObjectName")
{
//Do whatever you want
}
}
}
Look that when you get the data from collision is in a "Collider" type and you acces the gameObject properties of this collider...... one of the game object properties is "name" others are "CompareTag" ,"GetComponent", "GetComponentInChildren" and so on
you can also acces other properties of the collider itself
Vector3 triggerPosition = new Vector3 (1.0f,2.3f,4.3f); // Storing some position in space
void OnCollisionEnter(Collision dataFromCollision)
{
if (dataFromCollision.transform == triggerPosition){
//Not very useful but here it does something when you collide with something in this exact triggerPosition
}
}
I truly recomend you to use this method, is one of unity´s most powerful tools. you can activate a scene when we pass thru a collider or maybe make an explosion, a jump scare, a portal when we hit a door with a special tag, a respawn if you get out of level.... etc
This sounds great, thanks for the tip, and yeah that makes Goldidog123's suggestion make a lot more sense then. Cool, I will give it a try and let you know if it works!
Thank you very much, this worked out great, tags were definitely the issue, thanks for helping to clear that up, I've been bashing my head against a wall on this for hours.
Answer by Goldiedog123 · Jul 21, 2017 at 01:44 AM
HEY,
Okay so you want one to only collide with certain objects?
The bellow script will allow the gameobject with the script attached to ignore objects with a certain tag.
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "WHATEVER")
{
Physics.IgnoreCollision(collision.collider, GetComponent<Collider>());
}
}
}
Hope this helps
Unfortunately, this didn't seem to work for me. it wasn't triggering anything and tried switching collisions to trigger as well, and that just had errors riddled everywhere.
did you set up tags? this script ignores collisions with gameobjects with certain tag
Right, and I get that, however, it's the exact opposite of what I'm going for, looking to collide with 1 specific thing.