• 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 seanieempire · Oct 12, 2022 at 01:12 AM · tilemaptriggerscollisoncontacts

replacement for collision.contacts with triggers instead of colliders

I am new to unity and am making t$$anonymous$$s little platformer where I just started trying to add coins as a tilemap, I found a helpful video on destroying tiles but it only works with OnCollisionEnter, and since I am making coins I figured it has to be a trigger.

It stops working right at collision.contacts and says 'collider2D' does not contain a definition for 'contacts'

I'm looking for an alternative to collision.contacts but for OnTriggerEnter

here's the code:

 void OnTriggerEnter2D(Collider2D collision)  
 {  
     if (collision.gameObject.CompareTag("Player"))  
     {  
         Vector3 HitPosition = Vector3.zero;  
         foreach(ContactPoint2D $$anonymous$$t in collision.contacts) // <-- doesn't work with OnTriggerEnter...  
         {
             HitPosition.x = $$anonymous$$t.point.x - 0.001f * $$anonymous$$t.normal.x;
             HitPosition.y = $$anonymous$$t.point.y - 0.001f * $$anonymous$$t.normal.y;
                 destructableTilemap.SetTile(destructableTilemap.WorldToCell(HitPosition), null);
         }
     }
 }  

when you do ctrl + . it just says to change collision.contacts to collision.GetContacts... w$$anonymous$$ch also doesn't work
UPDATE:
Thank you for the reply, and it works(!), But it only will delete a coin upon touc$$anonymous$$ng it from the left, right or up, but if you are going down onto the coin it wont delete it,
here's the changed code:

 void OnTriggerEnter2D(Collider2D other)
 {
     Debug.Log(other.transform.position);
     if (other.gameObject.CompareTag("Player"))
     {
         Vector2 HitPosition = Vector2.zero;
         HitPosition = other.transform.position;
         {
             HitPosition.x = (HitPosition.x) - (0.01f * HitPosition.x);
             HitPosition.y = (HitPosition.y) - (0.01f * HitPosition.y);
             destructableTilemap.SetTile(destructableTilemap.WorldToCell(HitPosition), null);
             Debug.Log(HitPosition);
         }
     }
 }

I'm pretty sure it only isn't working because of the - (0.001f * HitPosition) because it seems to me that it will move the point where it deletes a tile a little up and to the left w$$anonymous$$ch is supposed to make sure it gets the right tile but from the top it doesn't work.
Again thank you for the reply

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by MUG806 · Oct 12, 2022 at 07:55 AM

In a trigger event, no physics collision actually occurs, so as you can see in the argument for your method, it gives you a Collider instead of a collision. Colliders don't have any contacts to check so you can't use them to determine the collision point. I would suggest just using the position of the collider you collided with, w$$anonymous$$ch you have named "collision", w$$anonymous$$ch is quite confusing and I would recommend against, but I'll use it in my example.

You could replace the entire for each loop with just t$$anonymous$$s:

 HitPosition = collision.transform.position;
Comment
Add comment · Show 1 · 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 seanieempire · Oct 13, 2022 at 09:50 PM 0
Share

I updated my question and also that was helpful @MUG806

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

150 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

Related Questions

How to delete a Tile on Collision ? 0 Answers

Destroy Tiles That Collide With Object 1 Answer

Trying to make object turn red OnTriggerEnter 0 Answers

Weird problem with triggers 0 Answers

Why does my GameObject only trigger once? 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