- Home /
Make a gameobject Trigger Trigger Colliders but not Other Colliders
I have searched everywhere I know of for this answer and as far as I can tell, it is impossible. My game has a character with a collider on it. He shoots a lazer beam with a trigger collider at the end of it so it can sense what it touches but still go through colliders. I want to make a gameobject that will stop the beam but will not stop the player. The easiest way to do this would be to make the gameobject a trigger so that the character will pass through it. But as far as I can find, a trigger can not trigger a trigger so it would not stop the beam. Please help me.
Answer by NinjaISV · Jan 10, 2017 at 05:06 PM
A trigger will trigger another trigger as long as one of the GameObjects that is colliding has a Rigidbody attached to it.
Another way for this that would work though would be to put the player on a separate layer that ignores collisions with the GameObjects that are blocking you from moving. This would disable all triggers between the two layers as well though.
And one more option for you if those don't work for you, you can loop through all the Colliders and make them ignore collision with the player Collider. Code snippet:
Collider[] playerColliders;
Collider[] ignoreColliders;
void Awake () {
//Gets all the Colliders attatched to the player
playerColliders = GetComponents<Collider>();
//Gets all other colliders in the scene that are active.
//You will probably want to replace this with another method of getting just the ones you want to ignore.
//Or just loop through these and remove ones that don't have a certain tag, etc...
ignoreColliders = FindObjectsOfType<Collider>();
}
void IgnoreColisions () {
//Loops through all the colliders on the player.
foreach(Collider playerColl in playerColliders) {
//Ensures that the colliders attached to the player is not null and that it is not a trigger.
if (playerColl != null && playerColl.isTrigger == false) {
//Loops through all the other colliders we with to ignore.
foreach(Collider ignoreColl in ignoreColliders) {
//Ensures that the colliders we have gathered are not null and that they are not a trigger.
if (ignoreColl != null && ignoreColl.isTrigger == false) {
//Tells the physics engine to ignore collisions on both of these colliders, but just for eachother.
Physics.IgnoreCollision(playerColl, ignoreColl, true);
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Player movement 1 Answer
OnTriggerEnter2D and ladder won't be compatible 2 Answers
Ground in 2D Platformer Skewing 0 Answers
RigidBody2D mouse going through Collider ,Rigidbody2D move with mouse 1 Answer