2D Toolkit Forum
2D Toolkit => Support => Topic started by: adam on March 24, 2014, 10:26:48 pm
-
I have been using the following code for an isometric tilemap and getting the sprite to lock onto tiles but it is missing some tiles so though i would post here and see if someone can help
below is a small example of tiles x it can lock on but 0 it misses
x x x
0 0
x x x
the code we are currently using is as follows
public float xwid = 1.3f; width of the tilemaps
public float xheig = 0.45f; height of the tilemaps
x = Mathf.Round(transform.position.x / xwid) * xwid;
y = Mathf.Round(transform.position.y / xheig) * xheig;
z = transform.position.z;
transform.position = new Vector3(x, y, z);
thanks in advance.
-
You can just use the built in functions to do this -
int x, y;
tilemap.GetTileAtPosition(transform.position, out x, out y);
then
tilemap.GetTilePosition(x, y);
to snap to the closest tile. If you want to optimise it, refer to the code for the 2 functions - it should be obvious what to do there.
-
ok I am still learning unity and tried doing what you suggested with the following code
int a, b;
temp.GetTileAtPosition(transform.position, out a, out b);
Vector3 newposy = temp.GetTilePosition(a, b);
x = newposy.x;
y = newposy.y;
z = transform.position.z;
transform.position = new Vector3(x, y, z);
Unfortunately this does not seem to work at all..any ideas where we went wrong?
-
What is the error? I don't have enough info to tell you whats wrong. If you post an example unity package demonstrating why it isn't working will help.