Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Cannot set rotation of instantiated GO when placing Tile on grid

Discussion in '2D' started by baraujo, Jun 15, 2018.

  1. baraujo

    baraujo

    Joined:
    Aug 16, 2013
    Posts:
    1
    Hello,

    I'm using Unity 2018.1.2f1 64bit on Win10, and I'm using the Tilemap system to create tiles that have a BoxCollider2D on one of the sides. I've created a subclass of the Tile class where I place the GO containing the collider in the Instantiated Game Object field and the StartUp() method sets the position and rotation of the GO to the correct values, according to the position and rotation of the tile in the grid.

    However, I can't obtain the current rotation of the placed tile anywhere, just the position; the rotation is always the same no matter how I rotate the tile when placing on the grid. Below is the relevant code block:

    Code (CSharp):
    1. public class Stage1EnergyBlock : Tile {
    2. (...)
    3.     public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go) {
    4.         GetGrid(tilemap);
    5.         var tileWorldPosition = m_Grid.CellToWorld(position);
    6.         go.transform.position = new Vector3(
    7.             tileWorldPosition.x + 0.5f,
    8.             tileWorldPosition.y + 0.5f,
    9.             tileWorldPosition.z); //position works fine
    10.         go.transform.rotation = transform.rotation; // this is where the rotation should be
    11.         color = Color.white;
    12.         return base.StartUp(position, tilemap, go);
    13.     }
    14. }
    Am I missing something obvious? Or this is just not possible at the moment? The image below shows the problem: the first wall segment (top-left) is correct, the other ones are rotated and the associated GO does not rotate with them.
    colliderswrong.PNG
     
  2. ajtracy300

    ajtracy300

    Joined:
    Mar 8, 2014
    Posts:
    2
    bump. I have the same problem

    [Edit]
    After some playing around I came up with this:
    Code (CSharp):
    1. public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
    2.     {
    3.         TileBase tile = tilemap.GetTile(position);
    4.        Matrix4x4 matrix = tilemap.GetTransformMatrix(position);
    5.         go.transform.rotation = matrix.rotation;
    6.  
    7.         return base.StartUp(position, tilemap, go);
    8.     }
     
    Last edited: Oct 21, 2018
    Abrakam and Mr_Purple like this.
  3. Mr_Purple

    Mr_Purple

    Joined:
    Apr 2, 2018
    Posts:
    4
    Thank you :) you saved my day!! I had the same Problem, and i can't figured it out. Looking a week for a solution!
    That's it.