I cannot change the center of rotation of a Tilemap (Unity + C#)

Asked

Viewed 77 times

0

I’m developing a 2D game using the Unity 2018.3.02. I’m using Tilemap for the construction of the phase, but this Tilemap will rotate. The Tiles in Tilemap form a square, that is, the filled Tiles are those of the square border.

I have a script for my tilemap that makes this tilemap Rotacione when I click the right or left arrow keys on the keyboard:

if (Input.GetKeyDown(KeyCode.RightArrow)) transform.Rotate(0, 0, -90);
else if (Input.GetKeyDown(KeyCode.LeftArrow)) transform.Rotate(0, 0, 90);

The problem is that my tilemap rotates around the first grid Tile(0 in x and 0 in y), and I would like it to rotate around the center of the square formed by Tiles in tilemap.

I managed to make the center of the square be in position x=0,y=0, In this way, the tilemap rotates as planned. But how would I like to use the grid to get positions inside the square if I put the center in x=0,y=0, would be bad because there would be negative numbers in x and y inside the grid, so it would not be my favorite choice.

There is a way to change the center of rotation to a position other than the initial one (x = 0 and y = 0)?

1 answer

1

I don’t know much about Tilemap, but I’ve had a lot of problems with UI rotation. What I can suggest is a trick to help you with that.

Create a GameObject, position it in the center of your tilemap and throw the tilemap into it. Now when you rotate the GameObject, your Tilemap will rotate according to the rotation center of the GameObject father.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.