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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Tilemap Grid issue

Discussion in '2D' started by DragonFhangDev, Nov 2, 2020.

  1. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    Hey guys. so I have an interesting issue that I can't seem to fix here.

    Everything was working fine with my player spawning in and camera bounds set properly, then I moved my grid to a new area because the UI canvas was blocking it's view.

    Now my player nor camera load in where they are supposed to and it acts like my grid is in the old spot and I've checked everything out and I have no idea what else to do, so I figured I'd check in here.

    It looks like it's not updating the tilemap bounds even though it's scripted and I've compressed the tilemap.
     
    Last edited: Nov 2, 2020
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Hi, could you share more details of the Grid/Tilemap component and screenshots of the issues you are facing? Thanks!
     
  3. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    So sorry for the delay, completely forgot about this. I never initially moved the parent object, I only moved the child objects. So I'm not sure if that's what caused the issue.
     
  4. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    If you can reproduce this issue and share more details about it, do let us know! Thanks!
     
  5. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    So basically what happens is when I move the parent grid Unity is not setting the new tilemap bounds.

    Even if I was to create a new grid in a different place it still acts like the bounds are in the old area.

    I also use a camera controller that locks onto my player and my player has a setbounds script attached


    Code (CSharp):
    1. public void SetBounds(Vector3 botLeft, Vector3 topRight)
    2.     {
    3.         bottomLeftLimit = botLeft + new Vector3(.5f, 1f, 0f);
    4.         topRightLimit = topRight + new Vector3(-.5f,-1f, 0f);
    5.     }
    Camera Controller Script
    Code (CSharp):
    1. void Start()
    2.     {
    3.         instance = this;
    4.  
    5.         //target = PlayerController.instance.transform;
    6.         target = FindObjectOfType<PlayerController>().transform;
    7.  
    8.         halfHeight = Camera.main.orthographicSize;
    9.         halfWidth = halfHeight * Camera.main.aspect;
    10.  
    11.         theMap.CompressBounds();
    12.         bottomLeftLimit = theMap.localBounds.min + new Vector3 (halfWidth, halfHeight, 0f);
    13.         topRightLimit = theMap.localBounds.max + new Vector3 (-halfWidth, -halfHeight, 0f);
    14.  
    15.         PlayerController.instance.SetBounds(theMap.localBounds.min, theMap.localBounds.max);
    16.     }
    17.  
    18.     // LateUpdate is called once per frame after Update
    19.     void LateUpdate()
    20.     {
    21.         transform.position = new Vector3(target.position.x, target.position.y, transform.position.z);
    22.  
    23.         //Keep the camera inside the bounds
    24.         transform.position = new Vector3(Mathf.Clamp(transform.position.x, bottomLeftLimit.x, topRightLimit.x), Mathf.Clamp(transform.position.y, bottomLeftLimit.y, topRightLimit.y), transform.position.z);
    25.  
    26.         if(!musicStarted)
    27.         {
    28.             musicStarted = true;
    29.             AudioManager.instance.PlayMusic(musicToPlay);
    30.         }
    31.     }
     
  6. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
  7. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    Ah, thank you for the reply.

    I'm guessing I need to use LocalToWorld? Still kinda new to scripting so just need a point in the right direction please :)
     
  8. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Yes, you can use
    Tilemap.LocalToWorld
    to convert each point in the bounds to world-space.
     
  9. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    I don't understand why I can't get it to work. No idea what I'm missing.

    I can change it to LocalToWorld, but I get an error with it saying that it's a method which is not valid in the given context.
     
  10. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Would it be possible to share the error as well as the code block here? You should be able to use LocalToWorld for your Tilemap component "theMap".
     
  11. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    public Tilemap theMap
    Well I tried changing this

    bottomLeftLimit = theMap.localBounds.min + new Vector3 (halfWidth, halfHeight, 0f);
    topRightLimit = theMap.localBounds.max + new Vector3 (-halfWidth, -halfHeight, 0f);


    To this

    bottomLeftLimit = theMap.LocalToWorld.min + new Vector3 (halfWidth, halfHeight, 0f);
    topRightLimit = theMap.LocalToWorld.max + new Vector3 (-halfWidth, -halfHeight, 0f);


    I get this error here.
    GridLayout.LocalToWorld(Vector3)' is a method, which is not valid in the given context
     
  12. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Ah, I see. You should be using Tilemap.LocalToWorld to convert a coordinate from local to world space, for example:

    Code (CSharp):
    1. bottomLeftLimit = theMap.LocalToWorld(theMap.localBounds.min) + new Vector3 (halfWidth, halfHeight, 0f);
    2.  
     
  13. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32

    Ok, it works, but I'm still getting an issue. Here's what the camera does now as it doesn't follow him. The white box is the camera, it just jumps from area to area instead of smoothly following
    https://gyazo.com/b73aa969f7a08ef769c2b647f8ef599f

    Camera Controller script

    theMap.CompressBounds();
    bottomLeftLimit = theMap.LocalToWorld(theMap.localBounds.min) + new Vector3(halfWidth, halfHeight, 0f);
    topRightLimit = theMap.LocalToWorld(theMap.localBounds.min) + new Vector3 (-halfWidth, -halfHeight, 0f);

    PlayerController.instance.SetBounds(theMap.localBounds.min, theMap.localBounds.max);


    Player Controller script.

    public void SetBounds(Vector3 botLeft, Vector3 topRight)
    {
    bottomLeftLimit = botLeft + new Vector3(.5f, 1f, 0f);
    topRightLimit = topRight + new Vector3(-.5f,-1f, 0f);
    }


    Are these both right still?
     
  14. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Could you share how you are moving the camera?

    You may want to add debug Inspectors for your Camera and Player Controllers to see if the values are updated correctly as you move your Player character.
     
  15. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32

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

    public class CameraController : MonoBehaviour
    {
    public static CameraController instance;
    public Transform target;

    public Tilemap theMap;
    private Vector3 bottomLeftLimit;
    private Vector3 topRightLimit;

    private float halfHeight;
    private float halfWidth;

    public int musicToPlay;
    private bool musicStarted;

    // Start is called before the first frame update
    void Start()
    {
    instance = this;

    //target = PlayerController.instance.transform;
    target = FindObjectOfType<PlayerController>().transform;

    halfHeight = Camera.main.orthographicSize;
    halfWidth = halfHeight * Camera.main.aspect;

    theMap.CompressBounds();
    bottomLeftLimit = theMap.LocalToWorld(theMap.localBounds.min) + new Vector3(halfWidth, halfHeight, 0f);
    topRightLimit = theMap.LocalToWorld(theMap.localBounds.min) + new Vector3 (-halfWidth, -halfHeight, 0f);

    PlayerController.instance.SetBounds(theMap.localBounds.min, theMap.localBounds.max);
    }

    // LateUpdate is called once per frame after Update
    void LateUpdate()
    {
    transform.position = new Vector3(target.position.x, target.position.y, transform.position.z);

    //Keep the camera inside the bounds
    transform.position = new Vector3(Mathf.Clamp(transform.position.x, bottomLeftLimit.x, topRightLimit.x), Mathf.Clamp(transform.position.y, bottomLeftLimit.y, topRightLimit.y), transform.position.z);

    if(!musicStarted)
    {
    musicStarted = true;
    AudioManager.instance.PlayMusic(musicToPlay);
    }
    }
    }


    I can definitely hook up some Debug lines and see what's going on as well.
     
  16. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    Sounds good!

            topRightLimit = theMap.LocalToWorld(theMap.localBounds.min) + new Vector3 (-halfWidth, -halfHeight, 0f);


    I guess the topRightLimit should be the maximum bounds of your Tilemap?
     
  17. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    Yep! those are the max limits corner to corner
     
  18. DragonFhangDev

    DragonFhangDev

    Joined:
    Oct 22, 2019
    Posts:
    32
    Do you guys have any idea why this could be doing this? If I never move the tilemap's grid or child grids it's perfectly fine. But the minute I move any of those things it breaks.

    It works fine with the localBounds before moving the grid as well.
     
  19. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    As mentioned, adding Debug lines or a Debug inspector will help a lot to figure out the issue. The items you would need to check when you move the grids are: are the bottomLeftLimit and topRightLimit values correct, are the bounds of your PlayerController updated correctly and is the Camera's position clamped correctly?