Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Epicnez · Jun 05, 2020 at 12:30 AM · 2dcollisioninstantiatetilemapdestructible

Tile Collision Detection Not Always Working

Hello! Here's what I'm doing:

First the player clicks or presses "e" and a "rocket" is fired, it is a physics object with a circle collider 2d that is set as a trigger. When this hits the tile map it instantiates a "explosion."

This explosion has a circle collider 2d which is NOT set as a trigger. The explosion has this script attached to it:

 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);
     
     
         }
     
     }

In the inspector this script is at the top, right below the transform component. Below this script and the other components of the explosion is a script that destroys the circle collider 2d on the "explosion" after 0.03 seconds. So it has enough time to collide and destroy the tiles, but not so much that the player will be able to collide with it. Then I have another script that destroys the "explosion" after 3 seconds.

This works most of the time, but sometimes after the rocket is destroyed, Sometimes only the explosion spawns. Sometimes only the particles and sound effects spawn. And sometimes nothing spawns. Why is this? Thank you for any help, it is appreciated! Thank you again!

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

3 Replies

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

Answer by Epicnez · Jun 05, 2020 at 03:07 AM

Update: I got the projectile with no gravity scale working by increasing the size of the circle collider 2d on it. I'm sorry I didn't look into this further and try these things before posting this question. Thank you!

EDIT Note: The fix to part of this problem is also included in the previous update comment! Thanks again!

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
avatar image
0

Answer by Epicnez · Jun 05, 2020 at 02:21 AM

Update: I got it working correctly on the physics based projectile by changing the delay of disabling the circle collider 2D from 0.03 to 0.07. The projectile with no gravity scale will still occasionally not work. Also I know this question is really specific and I'm sorry about that but I'm just trying my best to find a solution. So thank you so very much for your help!

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
avatar image
0

Answer by Epicnez · Jun 05, 2020 at 03:07 AM

Update: I got the projectile with no gravity scale working by increasing the size of the circle collider 2d on it. I'm sorry I didn't look into this further and try these things before posting this question. Thank you!

EDIT Note: The fix to part of this problem is also included in the previous update comment! Thanks again!

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

338 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

The tilemap collider is simply not working 0 Answers

Modular Room System Similar to Mega Man 0 Answers

Collision with Texture when alpha=1 1 Answer

Tilemap collider doesn't appear 1 Answer

Tilemap collider 2D problem. 0 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