Hello Guest

Author Topic: Snapping sprite to grid  (Read 10348 times)

Artheus

  • Newbie
  • *
  • Posts: 19
    • View Profile
Snapping sprite to grid
« on: October 08, 2013, 05:32:50 am »
Hi all,

I want to place my sprites at the precise center of a tile, is there a way to do that?  My tiles are 32x32 pixels, but my sprites are various sizes.  Specifically, is there is a way to snap the anchor point of the sprite to the center of the tile?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Snapping sprite to grid
« Reply #1 on: October 08, 2013, 11:35:41 am »
If you make your tiles have a center anchor point, setting the sprite position = tile position will snap it to the center of the tile. If that isn't what you're asking, a picture would really help explain :)

Artheus

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Snapping sprite to grid
« Reply #2 on: October 08, 2013, 06:47:15 pm »
Yes, that is precisely what I'm asking.  Sorry if I'm being unclear, I'm new to Unity/2D Toolkit.

I want to make my game grid based, thus my sprites need to centered in each grid.

The tiles I'm using make up the background/grid.  I'm using 2D Toolkit's tile map objects to do that.  My grid squares are the same size as the tiles, 32x32 pixels.

Now here is what I'm trying to accomplish.  When I manually place a sprite on the scene, I want the anchor point of the sprite (usually the center of the sprite) to snap to the center of the tile I place it on.  Is there a way I can code that to happen automatically? 

Now, on to what you said.  Could you elaborate on that?  Some example code would be awesome.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Snapping sprite to grid
« Reply #3 on: October 08, 2013, 06:52:31 pm »
I dont think you need any specific code for this. Just make sure the anchor point in the sprite collection editor is where you'd like them to match up. Just set the position to be exactly the same as the grid position. tilemap.GetTilePosition(x, y) gives you the worldPosition of an x / y tile.

Artheus

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Snapping sprite to grid
« Reply #4 on: October 08, 2013, 07:07:58 pm »
Where should I do this?  I don't see any options in the editor itself, are you saying I should do this when I run the game?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Snapping sprite to grid
« Reply #5 on: October 08, 2013, 07:09:44 pm »
Set it up in the sprite collection editor, so that the 2 sprites, set at 0, 0, 0 appear the way you want it to. Then at runtime, get the tile map.GetTilePosition to get the position you need to set to the sprite.

Artheus

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Snapping sprite to grid
« Reply #6 on: October 08, 2013, 07:21:47 pm »
Ok, I see what your saying. 

I have all the anchor points set up for my tiles and sprites.  I should place my sprites approximately where I want them to go, and then during run time, I should check which tile the is sprite is standing over, and snap it to the center of that tile.

I thought about it doing it this way, but I actually wanted to change the way the editor worked so my sprites would snap to the center of the tile when I place them, before run time.  That way, I can see exactly how they would look.  Would it possible if I subclassed the TK 2d sprite object?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Snapping sprite to grid
« Reply #7 on: October 08, 2013, 07:28:36 pm »
You shouldn't need to subclass, just attach a script with an [ExecuteInEditMode] attribute, and snap to the closest grid point from there.

Artheus

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Snapping sprite to grid
« Reply #8 on: October 08, 2013, 07:32:04 pm »
Perfect, that's all I need for now.  Thanks alot.