Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 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
0
Question by kfreeves · May 03 at 09:17 PM · destroytilemapgridprojectiletank

Rescaling of grid and tilemap causes it not to destruct properly

Hello,

I am currently having an issue with my 2D unity game where changing the transform scale of my grid used for my tilemap, causes the scripts that destruct my tiles not to work properly. I've included pictures below of what it looks like when it is working correctly and how it is behaving now after I set the grid transform scale from 1 to 0.5. I'm currently thinking my issue lies somewhere within my TerrainDestroyer.cs script or my Projectile.cs script where I mention "radius" (radius of explosion). Any help would be greatly appreciated.

 TerrainDestroyer.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Tilemaps;
 
 public class TerrainDestroyer : MonoBehaviour
 {
     public Tilemap terrain;
     public Tilemap destroyedTerrain;
     public Tile explodedTile;
     GameObject debreePrefab;
     public static TerrainDestroyer instance;
 
     private void Awake()
     {
         instance = this;
     }
     public void DestroyTerrain(Vector3 explosionLocation, float radius)
     {
         for (int x = -(int)radius; x < radius; x++)
         {
             for (int y = -(int)radius; y < radius; y++)
             {
                 if (Mathf.Pow(x, 2) + Mathf.Pow(y, 2) < Mathf.Pow(radius, 2))
                 {
                     Vector3Int tilePos = terrain.WorldToCell(explosionLocation + new Vector3(x, y, 0));
                     if (terrain.GetTile(tilePos) != null)
                     {
                         DestroyTile(tilePos);
                     }
                 }
             }
         }
     }
 
     void DestroyTile(Vector3Int tilePos)
     {
         terrain.SetTile(tilePos, null);
         destroyedTerrain.SetTile(tilePos, explodedTile);
         
         //SpawnDebree(tilePos);
     }

[/CODE] End of TerrainDestroyer.cs

 Projectile.cs  
 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Audio;
 
 public class Projectile : MonoBehaviour
 {
 
     Rigidbody2D rb;
     
     public float AliveTime = 3;
     public float radius = 0.5f;
     //public GameObject ExplosionPrefab;
     public GameObject explosionFX;  
     public GameObject ProjectileGO;
     public BattleSystem system;
     public AudioClip clip;
     public OptionsMenu optionsMenu;
 
     public Player playerone;
     public PlayerTwo playertwo;
 
     // public AudioMixer FXaudioMixer;
     // public Slider slider;
     // public GameObject fxvolume;
 
     void Awake()
     {
         rb = GetComponent<Rigidbody2D>();
         Invoke("Explode", AliveTime);
         Invoke("EnableCollider", .2f);
         system = GameObject.Find("BattleSystem").GetComponent<BattleSystem>();
         // playerone = GameObject.Find("Player").GetComponent<Player>();
         // playertwo = GameObject.Find("PlayerTwo").GetComponent<PlayerTwo>();
         //optionsMenu = slider.GetComponent<OptionsMenu>();
         //optionsMenu = GameObject.Find("OptionsMenu").GetComponent<OptionsMenu>();
         //slider = GameObject.Find("Sound Effect Volume Slider").GetComponent<Slider>();
         //slider = optionsMenu.GetComponent<Slider>();
     }
 
     void Update()
     {
         Vector2 v = rb.velocity;
         float angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
     }
 
     void EnableCollider()
     {
         GetComponent<Collider2D>().enabled = true;
     }
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         //if collsion with the wind box, return
         if (collision.gameObject.name == "Wind")
         {
             Debug.Log("Colliding with wind");
         }
         else
         {            
             Explode();
         }
     }
 
     public void Initialize(int power)
     {
         rb.AddForce(transform.right * (power / 2), ForceMode2D.Impulse);
     }
 
     void Explode()
     {
         TerrainDestroyer.instance.DestroyTerrain(transform.position, radius);
         SpawnExplosionFX();
         
         // DoCameraShake();
         //Camera.main.GetComponent<CameraShake>().shakeDuration = 0.2f;
         Destroy(ProjectileGO);
     }
 
     void SpawnExplosionFX()
     {
         GameObject insExpl = Instantiate(explosionFX, transform.position, Quaternion.identity);
         insExpl.transform.localScale *= radius;
         Destroy(insExpl, .2f);
     } 
 
     void DoCameraShake()
     {
         Camera.main.GetComponent<CameraShake>().shakeDuration = 0.2f;
     }
 
     public float soundlevel()
     {
         float value;
         bool result = optionsMenu.FXaudioMixer.GetFloat("soundFXvolume", out value);
         if(result) 
         {
             return value;
         }
         else
         {
             return 0f;
         }
     }
     
 
     private void OnDestroy()
     {
         //currently this doesnt change with the sound fx volume slider
         //z-axis directly inpacts the volume level
         //int z = optionsMenu.SetFXVolume();
         //int z = FXaudioMixer.ClearFloat();
         // float value;
         // bool result = optionsMenu.FXaudioMixer.GetFloat("soundFXvolume", out value);
         AudioSource.PlayClipAtPoint(clip, new Vector3(21,5, -20));
         
         if (system.state == BattleState.PLAYERONETURN)
         {
             //playertwo.currentFuel = playertwo.maxFuel;
             system.state = BattleState.PLAYERTWOTURN;
         }
         else if (system.state == BattleState.PLAYERTWOTURN)
         {
             //playerone.currentFuel = playerone.maxFuel;
             system.state = BattleState.PLAYERONETURN;
         }
     }
 
     
 
 }


After explosion (working with normal grid scale) (explosion projectile from tank exploded in middle causing that crater) alt text

After explosion (not working with altered grid scale) (explosion happened just left of two tile high pillar, causing unusual semi-hole) alt text

afterexplode.png (195.2 kB)
afterexplodebad.png (131.9 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

145 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

Related Questions

How can i make the Unity2d grid use rounded numbers? 1 Answer

Tilemap Grid Broken? 1 Answer

How can I access to a tile properties in a Tilemap? 0 Answers

Destructible tank? 1 Answer

Is there a way to change Tilemap Grid axis direction? 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