Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LegendaryCreator · Feb 18, 2019 at 10:36 PM · collision2d gameshooter

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LegendaryCreator · Feb 19, 2019 at 09:55 PM 0
Share

Isn't multiple colliders inefficient i would like to keep it as clean and fast as possible

avatar image zereda-games LegendaryCreator · Feb 19, 2019 at 09:57 PM 0
Share

look up Data Persistence and Object Pooling then i guess.

links:

1. Data Persistence

2. Object Pooling

avatar image LegendaryCreator zereda-games · Feb 20, 2019 at 11:44 PM 0
Share

This will be useful later but doesn't solve the problem.

avatar image LegendaryCreator · Feb 21, 2019 at 12:01 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LegendaryCreator · Feb 19, 2019 at 08:11 PM 0
Share

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);
         }
     }
 }
avatar image zereda-games LegendaryCreator · Feb 19, 2019 at 08:40 PM 0
Share

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?

avatar image LegendaryCreator zereda-games · Feb 19, 2019 at 08:54 PM 0
Share

No I'm currently $$anonymous$$ching myself

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

170 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Can I have two colliders attatched to my enemy that do different things and if so how?,Is it possible to have two colliders for one object 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges