- Home /
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)
After explosion (not working with altered grid scale) (explosion happened just left of two tile high pillar, causing unusual semi-hole)
Your answer
Follow this Question
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