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.
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.
Tilemap.localBounds is in the local-space of the Tilemap GameObject (Unity - Scripting API: Tilemaps.Tilemap.localBounds). You will need to convert it to world-space if you are not tracking positions relative to the Tilemap GameObject.
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);
}
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.
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.
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.
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?