Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Epicnez · May 10, 2020 at 05:46 AM · collider2dtilemapcollision2dtilescontacts

Destroy Tiles That Collide With Object

Hello! Sorry to ask a question that some others have asked but I have not seemed to be able to find a solution. Here's what I have so far:

 using System.Collections;
 using System.Collections.Generic;
 using System.Numerics;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class DestroyTilesOnCollision : MonoBehaviour
 {
 
     public Tilemap tilemap;
     public ContactPoint2D[] contacts;
 
     void OnTriggerEnter2D(Collider2D collision)
 
     {
 
         if (collision.gameObject == tilemap)
 
         {
 
             collision.GetContacts(contacts);
             UnityEngine.Vector3 hitPosition = UnityEngine.Vector3.zero;
             foreach (ContactPoint2D hit in contacts)
 
             {
 
                 hitPosition.x = hit.point.x - 0.01f;
                 hitPosition.y = hit.point.y - 0.01f;
                 tilemap.SetTile(tilemap.WorldToCell(hitPosition), null);
             
             }
         
         }
     
     }
 
 }

Some additional information:

The way I am using this is by instantiating a game object with a circle collider 2D on it, which would then destroy the tiles it comes in contact with. (Is trigger is not enabled on the circle collider 2D since I understand that collision.GetContacts does not get any collision points if is trigger is enabled.) Other answers I have found on this subject use .contacts, but as that does not work, I believe that it has been deprecated, though I am not sure, and I know it causes a lot of memory garbage too. The documentation on .contacts says to use .GetContacts to reduce this. I replaced .contacts with setting an array to collision.GetContacts then using a foreach loop to iterate through that and disable the tiles.

Why is this not working? Any help is appreciated. Thank you!

Comment
Add comment · Show 2
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 Tehemus · May 10, 2020 at 01:01 PM 0
Share

have you tried without the world to cell ? I achieved it without it a while ago by flooring the positions according to the tilemap position.

avatar image Epicnez Tehemus · May 10, 2020 at 05:23 PM 0
Share

@Tehemus Thank you, but I cannot remove the world to cell without getting an error.

 tilemap.SetTile(tilemap(hitPosition), null);

Gives me this error: Non-invokable member 'DestroyTilesOnCollision.collision' cannot be used like a method.

 tilemap.SetTile(hitPosition, null);

Gives me this error: Argument 1: cannot convert from 'UnityEngine.Vector3' to 'UnityEngine.Vector3Int'.

1 Reply

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

Answer by Epicnez · Jul 06, 2020 at 04:34 AM

@Tehemus Got tile destruction working with this script:

 using System.Collections;
 using System.Collections.Generic;
 using System.Numerics;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class DestroyTilesOnCollision : MonoBehaviour
 {
 
     //public Tilemap tilemap;
     public ContactPoint2D[] contacts = new ContactPoint2D[10];
 
     public GameObject particles;
 
     public AudioSource explodeAndHitNoise;
 
     void OnCollisionStay2D(Collision2D collision)
 
     {
 
         Debug.Log("Hit!");
 
         if (collision.gameObject.name == "Tilemap")
 
         {
 
             Debug.Log("Hit tilemap!");
 
             int contactCount = collision.contactCount;
             if (contactCount > contacts.Length)
                 contacts = new ContactPoint2D[contactCount];
             collision.GetContacts(contacts);
 
 
             UnityEngine.Vector3 hitPosition = UnityEngine.Vector3.zero;
             for (int i = 0; i != contactCount; ++i)
 
             {
 
                 hitPosition.x = contacts[i].point.x;
                 hitPosition.y = contacts[i].point.y;
                 collision.gameObject.GetComponent<Tilemap>().SetTile(collision.gameObject.GetComponent<Tilemap>().WorldToCell(hitPosition), null);
                 var newParticles = Instantiate(particles, hitPosition, UnityEngine.Quaternion.identity);
                 var newSoundEffect = Instantiate(explodeAndHitNoise, this.transform.position, this.transform.rotation);
                 StartCoroutine(DestroyParticles(newParticles));
             
             }
         
         }
     
     }
 
     public IEnumerator DestroyParticles(GameObject particles)
 
     {
 
         yield return new WaitForSeconds(3f);
         Destroy(particles);
 
 
     }
 
 }

A redditor helped me get this working from the script in the initial question. Here's the reddit post: https://www.reddit.com/r/Unity2D/comments/gtqkuz/help_with_destroying_tiles_on_collision/ Thanks!

Comment
Add comment · 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

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

133 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

Related Questions

How to the detect what side of the player is colliding with a object in a tilemap 2 Answers

Stuck Between tilemap colliders 0 Answers

Tilemap Collider 2D not being created at runtime 0 Answers

Composite Collider on Tilemap not really stopping player 0 Answers

How to detect on which exact tile a collision happened? 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