Hello Guest

Author Topic: Locking onto tile with isometric tilemap  (Read 4745 times)

adam

  • Newbie
  • *
  • Posts: 8
    • View Profile
Locking onto tile with isometric tilemap
« 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Locking onto tile with isometric tilemap
« Reply #1 on: March 25, 2014, 10:38:27 am »
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.

adam

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Locking onto tile with isometric tilemap
« Reply #2 on: March 25, 2014, 03:57:49 pm »
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?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Locking onto tile with isometric tilemap
« Reply #3 on: March 26, 2014, 10:28:24 am »
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.