- Home /
Detecting what object is colliding with another in 2D Making a grid based game, and need to know which tile the player is standing on
I'm currently making a grid based rpg game, where you tap on a tile and go there, and I've found a very useful tutorial on the mater. The only problem is that he does it in 3D, and I'm doing it in 2D. Normally I'm able to change any 3D actions into 2D rather simply, but I'm having trouble figuring out how to detect which tile the player is currently on. He did it with a raycast, pointing down, and while I've tried to get that to work, it's come to no avail, so I started to look at colliders, but I can't figure out how to get it to work out properly. Simply, I need to detect which tile the player is colliding with, not if the player is colliding with any tile The video I'm following for reference is: https://www.youtube.com/watch?v=2NVEqBeXdBk&t=772s and he talks about the paticular problem (finding the target tile) 9:00 into the video. Thank you for your time, any help is greatly apretiated
Answer by sean244 · Jan 01, 2019 at 10:54 PM
Forget about colliders. If you attach the below script to your Player, it should do exactly what you want it to do. Basically, you need to convert the mouse position to world point, and then convert that world point to a grid position. Then convert that grid position back to world point. Then we add half the value of the cell size to both the x and y values to get the center of that grid position. Then, just call a Vector2.MoveTowards. Btw, the variables 'baseTilemap' and 'cellSize' should be in another class, maybe a singleton. I just put everything in the Player script so it's easier for you to see.
using UnityEngine;
using UnityEngine.Tilemaps;
public class Player : MonoBehaviour
{
/// <summary>
/// This variable should be inside of a singleton, but we'll put it in here for now. This is the tilemap that's a child of your 'Grid' object. Drag and drop it from the hierarchy to the inspector.
/// </summary>
[SerializeField]
private Tilemap baseTilemap;
private Vector2 cellSize, target;
private float speed = 3f;
private void Start()
{
//This should also be inside of a singleton.
cellSize = FindObjectOfType<Grid>().cellSize;
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
SetTarget();
}
if (target.magnitude > 0)
transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
private void SetTarget()
{
//Convert the click position to a world point.
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
//Convert that world point to a grid position.
Vector3Int gridPos = baseTilemap.WorldToCell(mousePos);
//Convert that grid position back to a world point.
target = baseTilemap.CellToWorld(gridPos);
//This is so we move to the center of the cell, not the lower left corner.
target.x += cellSize.x / 2;
target.y += cellSize.y / 2;
}
}
Thank you, this helps a bunch. I'm having trouble figuring out how to implement it. I get the error message: "error CS0246: The type or namespace name Tilemap' could not be found. Are you missing UnityEngine.Tilemaps' using directive?" I've added in the Grid, and put the code from [SerializeField] to the void start function, except for the speed variable, into a script inside Tile$$anonymous$$ap, which I wasn't fully sure if that's what I was supposed to do. I got the same error message when I had it all in the player script as well. I also didn't understand by what you meant by "Drag and drop it from the hierarchy to the inspector." I assume this is where I'm having a problem. Thanks again for the help.
You didn't copy the entire code. There's a using statement at the top of the script that has to do with tilemaps. Also, what do you mean when you say you 'put the code from [SerializeField] to the void start function'? [SerializeField] pertains only to one variable: baseTilemap. We give that variable the [SerializeField] attribute so that we can assign it in the editor. It's like when you make a variable public, you can see it in the editor, right?
Thank you, it worked. I wasn't sure what you meant by this variable should be in a simpleton. And right after I posted I realized you meant to put the Tilemap into the drag and drop spot on the inspector, but that wasn't loading because I had the error. Anyway, thanks for the help!
Answer by triis4924 · Jan 01, 2019 at 09:51 PM
just detect the click position. So, for example, you have a click position of X: 4.24 Y:-6.69 and then you make it an int so that x is 4 and Y -7 and then you let your player walk to x 4 y -7
Answer by hexagonius · Jan 01, 2019 at 09:39 PM
using colliders in a grid based game is unnecessary. you KNOW where the player is by its x and y coordinates compared to the grid.
Your answer