Search Unity

Question Repeatedly Rotate tile in tilemap not working

Discussion in '2D' started by CantCodeLol, Apr 6, 2021.

  1. CantCodeLol

    CantCodeLol

    Joined:
    Apr 6, 2021
    Posts:
    2
    Hello all,
    I am trying to make a simple game taking inspiration from factorio, and was trying to add rotateable machines (like in factorio). What I was trying to achieve was that whenever you press 'R' whilst hovering over a tile, said tile rotates by 90 degree increments (e.g. 1 press = 90 degrees, 2 = 180 etc.). I quickly ecountered the following problem: I can rotate it once, if I try to do so again, it rotates by 1 degree or so and all further presses seem to have no effect whatsoever.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Tilemaps;
    3.  
    4. public class TileManager : MonoBehaviour
    5. {
    6.     Tilemap tilemap;
    7.     Vector3Int tilemapPos;
    8.  
    9.     // There is some other code here, for example drawing tiles, selecting which one to draw etc. just assumed it to be irrelevant
    10.  
    11.     if (Input.GetKeyDown(KeyCode.R))
    12.     {
    13.         tilemapPos = tilemap.WorldToCell(Camera.main.ScreenToWorldPoint(Input.mousePosition));
    14.         float rot = tilemap.GetTransformMatrix(tilemapPos).rotation.z;
    15.         Matrix4x4 matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, rot + 90f), Vector3.one);
    16.         tilemap.SetTransformMatrix(tilemapPos, matrix);
    17.         }
    I am assuming this has something to do with quaternions? I searched the internet and couldn't find a solution for this, so decided to ask it here. Any simple explanation of why this isn't working is also greatly appreciated.

    EDIT: I am aware that storing a variable for each tile's rotation is a valid solution, however the board begins at a smaller size, and I plan for it to get bigger, meaning this solution wouldn't work.

    EDIT EDIT: Actually this will work, I'm just an idiot. You can use a 2d array, and when the board gets bigger, add some elements at the start and end of both the big array and all subarrays.

    Thanks in advance!
     
    Last edited: Apr 6, 2021
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Code (CSharp):
    1. float rot = tilemap.GetTransformMatrix(tilemapPos).rotation.z;
    I think this may be a problem? This is a Quaternion and does not directly map to the Euler Angles you see in the Transform inspector.

    You could replacing the line with the following:
    Code (CSharp):
    1. float rot = tilemap.GetTransformMatrix(tilemapPos).rotation.eulerAngles.z;
    or
    Code (CSharp):
    1. float original = tilemap.GetTransformMatrix(tilemapPos).rotation.z;
    2. Matrix4x4 matrix = original * Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(0f, 0f, 90f), Vector3.one);
    3. tilemap.SetTransformMatrix(tilemapPos, matrix);
     
  3. CantCodeLol

    CantCodeLol

    Joined:
    Apr 6, 2021
    Posts:
    2
    Thank you! I implemented the first solution, worked perfectly. Much more compact than 2d arrays (and simpler too!) I don't know why I didn't see euler angles as it's the second auto complete option.
    Thanks again!