- Home /
Find Tilemap Player is Currently At
Hi there!
EDIT: The main question I have is "How would you get the name of the "Tilemap" (not tile) located at the player object location?" Below is a really long way of asking that.
I'm trying to create something similar to the Zelda II: Adventure of Link overworld, where the player traverses on a map and enemies spawn randomly. If the player collides w/ an enemy they are taken to a scene that corrisponds with the type of landscape they were on before the collision. So, if they were on a grassy spot, the scene would be grassy, if they were on a wooded spot, the scene would be wooded... etc.
For a visual idea of how this would work, see the following video from 1:25 to 1:55 (the commentator also talks about how this functions here) www.youtube.com/watch?v=6k2bn6vech0&list=PLC8EABAF343E1BD4E
I know that there are several elements that would make this work, and I think I can piece together most of it (I'm subpar at programming currently).
I'm using the Unity Tilemap System in Unity 2018.3
The part I'm having trouble with right now, is getting the tile type (Plains, Swamp, Forest, etc.) from the tiling system.
Here's what I've tried thus far (maybe one of these is partially my answer... I just don't know the next step):
Attempt #1 -
I found the following code snippet in the Unity Documentation:
https://docs.unity3d.com/ScriptReference/GameObject-tag.html
using UnityEngine;
public class CollisionGameObjectExample : MonoBehaviour
{
//Detect collisions between the GameObjects with Colliders attached
void OnCollisionEnter(Collision collision)
{
//Check for a match with the specified name on any GameObject that collides with your GameObject
if (collision.gameObject.name == "MyGameObjectName")
{
//If the GameObject's name matches the one you suggest, output this message in the console
Debug.Log("Do something here");
}
//Check for a match with the specific tag on any GameObject that collides with your GameObject
if (collision.gameObject.tag == "MyGameObjectTag")
{
//If the GameObject has the same tag as specified, output this message in the console
Debug.Log("Do something else here");
}
}
}
I know it's not on Tilemaps, but I thought I could maybe use the a Tilemap Collider 2D as the collider for the GameObject and attach the code to my player object, as most of my map terrains/environments are housed in their own Tilemap/GameObject (under the Grid), and then making Tilemap Colliders triggers. But, when I did this the character ceased to be able to pass through the terrain/environment and there were no Debug Logs firing on collision.
Attempt #2 -
I use the following code I've piecemealed together from documentation and forum posts:
private GameObject
playerObj = null;
Vector3 playerLocV;
float playerX = 0;
float playerY = 0;
float playerZ = 0;
void Start()
{
if (playerObj == null)
playerObj = GameObject.Find("OverworldCharacter");
}
void Update()
{
playerX = playerObj.transform.position.x;
playerY = playerObj.transform.position.y;
playerZ = playerObj.transform.position.z;
playerLocV = new Vector3(playerX, playerY, playerZ);
Vector3Int local = Vector3Int.FloorToInt(transform.InverseTransformPoint(playerLocV));
Tilemap map = GetComponent<Tilemap>();
TileBase tile = map.GetTile(local);
//Debug.Log(tile);
Debug.Log(map); //This returns the desired data type, but the information is not useful.
}
}
While this code does "work", it only works when I attach it to a (every) Tilemap object... and the information it outputs is not accurate or useful. As it spits out the name of every Tilemap object that has the script associated with it, regardless of whether the player object is near the Tilemap object or not.
EDIT: I've found that the information that is provided from 'map' is pretty much what I'm looking for as far as data type... it's still not accurate or useful, and I'm not sure how to manipulate the line of code for what I'm trying to do.
Ideally, I would have some sort of listener on the player object to see what TileMap/GameObject is directly beneath them... I'm just not sure how to get there from here.
Thanks for your help in advance.