Question by
HansenDJ · Feb 25, 2018 at 07:29 AM ·
2d2d spritestile
Change Single Tile Depending on Amount of Damage Done to Tile by Vehicle
I’m making a game where you have a Bulldozer and you have to crash into walls and they break after 3 or so hits. The walls and ground are tiles. I can get each wall tile hit, to change to a cracked wall sprite with some issues from offsetting it. I just can’t figure out how to make it change to another sprite after the next hit, and then break on the 3rd hit. I'll post my bulldozer script below, that is doing the tile changing. I left out the movement parts of the script to slim it down. Thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class Bulldozer : MonoBehaviour {
public GameObject destructableTilemap;
Tilemap tilemap;
public Tile borderTile;
public Tile destructableTile;
public Tile crackedTile;
// Use this for initialization
void Start ()
{
if (destructableTilemap != null)
{
tilemap = destructableTilemap.GetComponent<Tilemap>();
}
}
void OnCollisionEnter2D(Collision2D col)
{
Vector3 hitPosition = Vector3.zero;
if (tilemap != null && col.gameObject == destructableTilemap)
{
foreach (ContactPoint2D hit in col.contacts)
{
hitPosition.x = hit.point.x - 0.01f * hit.normal.x;
hitPosition.y = hit.point.y - 0.01f * hit.normal.y;
tilemap.SetTile(tilemap.WorldToCell(hitPosition), crackedTile);
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Applying Normal Maps to Random Tiles 0 Answers
How to get custom data from a tile 0 Answers
Check the if mouse clicked on specific tile 0 Answers
Why are the 2D colliders not working? 0 Answers
Game (2D) not rendering at all 2 Answers