Hey All!
I’ve been working with unity for a few weeks now, doing various tutorials and getting the hang of the editor. Now that I think I have a handle on the basics (very basics) I would like to work on a grid based click to move system for movement in my game. So basically, right now, I just want to be able to click on a tile on my map, and have the player appear there. Then eventually I’ll add in path finding so that the player actually moves through the spaces to the location.
My end goal is to actually make a clone of this DOS classic:
I’ve been using Tiled and Tiled2Unity to create my maps, done some movement with the arrow keys, and am hoping some of that can translate to mouse moving.
Any tutorials or pointers would be a great help!
You need to:
in Update() if Input.GetMouseButtonDown then get coordinates you clicked on using Camera.ScreenToWorldPoint and Input.mousePosition ; find x and y of tile you need to appear to from coordinates ; Set Transform-position to new coordinates (perhaps with offset so character is in tile and not between them). Oh and somewhere before moving it would be nice to check if you can appear on that tile or not.
Perhaps what I wrote is completely wrong because you don’t even tell us(forums I mean) how exactly did you do arrow keys movement.
Well, that’s it. Next learn some scripting to make that happen.
I’m too lazy to script for you.
Thanks for the pointers! I definitely didn’t expect the scripting to be done for me ;). The arrow movement is done by checking the key press in Update() and depending on the arrow pressed, add the size of the sprite’s bounding box to the current location to have it move. So currently the movement doesn’t take any of the actual tiled map into account.
So I updated my my CheckInput() function to check for mouse clicks and now I have:
private void CheckInput() {
if ((Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) || (Input.GetMouseButtonDown(0))) {
Vector3 game_coordinates;
Vector3 mouse_position = Input.mousePosition;
game_coordinates = Camera.main.ScreenToWorldPoint(new Vector3(mouse_position.x, mouse_position.y, 1f));
pos = game_coordinates;
moving = true;
}
}
Which pretty simply changes the pos (positon of the sprite) to the mouse click location. Now I need to figure out how to get the tile that was clicked so that I can center my player within that tile.
Um… I recommend deciding on size of tiles as fixed value (for example 1) and then addressing them as tile coordinate. Example: point in (1.3 , 2.8) is same as point in tile (1 , 2). Which means you just have to place your unit into (1 , 2) + offset(depends on sprite).
As for sprites, you can always scale them up/down so unit will match it in size.
With tileSize = 1f getting tile coordinate from world position coordinate is simple:
public static int Coord(float val)
{
return val<0?(int)val-1:(int)val;
}
There is a little case when float is negative: you need to decrease int by 1 then with such addressing otherwise 0.5f will give same tile coordinate as -0.5f.
Well, actually coordinate has both x and y and you apply that method to both, but I’m too lazy to write struct or open my Vector2i struct.
Thanks for your help! You definitely pointed me in the right direction and I got something up and running.