Hello Guest

Author Topic: Sprite displays incorrectly after changing via setSprite in script  (Read 2970 times)

Plethora22

  • Newbie
  • *
  • Posts: 2
    • View Profile
So for starters let me just say that I'm fairly new with Unity in general, and VERY new with 2dtk, so if I'm missing something obvious or simple I apologize, but this one has me stumped because it doesn't seem to make sense. 

Basically I wrote a simple script that creates a tilemap on which a character will move around (precursor to a random map generator, hence doing it via script).  Since I'm just learning all this, my first attempt just fills an 8x8 grid with grass tiles.  Simple, easy, and it works fine. 

Here's the Code:
Code: [Select]
public class mapInitScript : MonoBehaviour {
public GameObject Tile;

private tk2dSprite sprite;

// Use this for initialization
void Start () {
Vector2 dimensions;
Vector2 currentPos;
dimensions.x = dimensions.y = 8;
currentPos.x = currentPos.y = 0;

for(int i = 0; i < dimensions.x * dimensions.y; i++){
GameObject temp = Instantiate (Tile) as GameObject;
Vector3 coords;
coords.x = (float)(currentPos.x * 4);
coords.y = (float)(currentPos.y * 3);
coords.z = (float)10;
temp.transform.position = coords;
//if(temp.transform.position.y == 0){
// sprite = temp.GetComponent<tk2dSprite>();
// sprite.SetSprite("grasstop");
//}

currentPos.x += 1;
if(currentPos.x == dimensions.x){
currentPos.x = 0;
currentPos.y += 1;
}
}

}

The above works fine, and does exactly what I wish it to.  But notice that commented out portion.  When I uncomment that portion, it does in fact draw the correct tiles (the "grasstop" sprite is displayed), but they are shifted about half a tile down.  It leaves a strip of empty space between row 1 and row 2.  To be clear, the transform of the new sprite is not moved, the y value is still 0 like it should be.  For the sprite to be correct, I have to change it manually to a y value of 1.45 (a full tile has a height of 3 with my current setup).  This makes absolutely no sense to me.

Any thoughts?  If anyone wants more info I can take some screenshots, I'm just hoping there is something obvious I'm missing...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Sprite displays incorrectly after changing via setSprite in script
« Reply #1 on: November 05, 2013, 10:46:14 pm »
What is the anchor position of the sprite? The sprite geometry is created relative to the anchor point, so if it is different on the sprite, vs the tiles (eg. centre, with different sized sprites) it will be offset.

Plethora22

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Sprite displays incorrectly after changing via setSprite in script
« Reply #2 on: November 05, 2013, 10:51:00 pm »
Doh!  That's it, thanks for the quick reply!  I coulda sworn I selected all when I changed it to bottom-left anchor, but apparently not.

Still, what would game design be without the occasional story of personal idiocy.  :)