Home > Back-end >  How do I scale the specific tile on the tilemap?
How do I scale the specific tile on the tilemap?

Time:11-19

everyone! I'm making a game on Unity 2D and I encountered a problem. I need to dig specific tile of snow when player is holding LeftShift and is in the trigger with tag "Snow" (the tilemap does have such a tag). I decided to change the scale, since it's the most understandable for player of variants that i thought (making the sprite darker, destroying it, etc.) Right now I have this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class SnowTaking : MonoBehaviour
{   //Tile variables
    public Tilemap tilemap;
    public ITilemap iTilemap;
    public Tile whiteTile;
    public Tile redTile;
    public Tile greenTile;
    public Tile blueTile;

    void OnTriggerStay2D(Collider2D other) {
        if(other.gameObject.tag == "Snow") {
            if(Input.GetKey(KeyCode.LeftShift)) {
                //При нажатии LeftShift
                float x = gameObject.transform.position.x;
                float y = gameObject.transform.position.y;
                float z = 0;
                Vector3 position = new Vector3(x, y, z);
                Vector3Int tilePosition = tilemap.WorldToCell(position);
                Tile currentTile = tilemap.GetTile<Tile>(tilePosition);
                //Conditions for each type of snow
                if(whiteTile == currentTile) {
                    //Scaling tile here
                    Debug.Log("White");
                } else if(redTile == currentTile) {
                    //Scaling tile here
                    Debug.Log("Red");
                } else if(greenTile == currentTile) {
                    //Scaling tile here
                    Debug.Log("Green");
                } else if(blueTile == currentTile) {
                    //Scaling tile here
                    Debug.Log("Blue");
                } else {
                    Debug.Log("None");
                }
            }
        }
    }
}

What can I use to scale tiles? Thank you in advance!

I tried few things already:

  1. First of all, I searched through the net (especially in the documentation) functions, that do something with the exact tile;
  2. Then I tried to use Matrix4x4 to scale tile, but it didn't work as intended (it didn't work at all, but at least there were no errors);
currentTile.transform = Matrix4x4.Scale(new Vector3(0.5f, 0.5f, 1));
  1. When I was out of options, I tried to do something myself, and used Sprites. Of course, it didn't work;
Sprite currentSprite = currentTile.sprite;

currentSprite.localScale -= new Vector3(0.01f, 0.01f, 0);
  1. Then I searched such a question here, on StackOverflow, but didn't find anything that would help me, so here my quesion is!

CodePudding user response:

You can use tilemap.SetTransformMatrix();

In your case, instead of currentTile.transform = Matrix4x4.Scale(new Vector3(0.5f, 0.5f, 1));

You can use tilemap.SetTransformMatrix(tilePosition, Matrix4x4.Scale(new Vector3(0.5f, 0.5f, 1)));

If you want to animate it :

void Update() {

    if (Input.GetKey(KeyCode.LeftShift)) {
      time = 0f;
    }
    if (time < scaleDuration) {
      time  = Time.deltaTime;
      var scaleValue = _animationCurve.Evaluate(time / scaleDuration);
      tilemap.SetTransformMatrix(tilePosition, Matrix4x4.Scale(new Vector3(scaleValue, scaleValue, 1)));

    }

  • Related