- Home /
How to have a player which is a trigger also make contact with regular colliders
I have a 2D Space shooter game similar to the Asteroids game. When the ship shoots a laser, the laser can hit another ship and destroy it. I did this by turning the ship collider into a trigger. But now it ignores other colliders how can a have colliders on the environment while still making sure the ship is hit with lasers. The code is kinda all over the place so I will provide as needed.
Answer by Cornelis-de-Jager · Feb 19, 2019 at 09:07 PM
You can have multiple Colliders on an object. You can have the first collider handling collisions and the second as a trigger which handles trigger events.
Isn't multiple colliders inefficient i would like to keep it as clean and fast as possible
look up Data Persistence and Object Pooling then i guess.
links:
This will be useful later but doesn't solve the problem.
This works I thought multiple colliders on one object was bad but after looking researching it seems that its a common thing, and its not a problem when one is a trigger and the other is not.
Answer by Chimer0s · Feb 19, 2019 at 01:38 AM
Why did you make the ship a trigger? You can still call OnTriggerEnter or similar functions on non-trigger colliders assuming they have a rigidbody attached and interact with a trigger. What would make the most sense to me (assuming you're not already doing it this way) is to have the laser be the object with the trigger collider as I imagine it won't be behaving according to physics when making contact with something. If you do that, you can still call OnTriggerEnter on the ship when it's hit by the laser.
Check out this page to find the collision matrix which will show you the circumstances under which each kind of collision is detected. It's very helpful with things like that.
P.S. You could theoretically have two colliders on the ship, one trigger and one that isn't, but I wouldn't recommend it if you can avoid it.
The ship is a trigger so I can call the OnTriggerEnter like you said when the instantiated laser hits anything with the Player Tag. The prefab for the Player is a parent GameObject which has the Rigidbody2D for movement and the child object is the ship sprite which has the Polygon Collider. I was originally trying to do collision only by tags but i cant understand the way to implement it.
This is the laser code if i turn "is Trigger" off, the code doesn't work. I don't understand, if the gameobject still has the rigidody and it detects collision why doesn't the code work when "is trigger" is off
public class Laser : $$anonymous$$onoBehaviour
{
public float Speed = 10;
public Rigidbody2D rb;
void Start()
{
rb.velocity = transform.up * Speed;
Destroy(gameObject, 1);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(other.gameObject);
}
}
}
Are you following the Udemy course "Complete C# Unity Developer 2D: Learn to Code $$anonymous$$aking Games" by chance if so i may be able to help?
No I'm currently $$anonymous$$ching myself
Your answer
![](https://koobas.hobune.stream/wayback/20220612191954im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Text never displays count variable 1 Answer
Top Down Shooter Player Controller 1 Answer
Destroy a specific prefab (GameObject) Unity2D 1 Answer
Trigger2D not working 0 Answers