How do I make the player only able to place object on a grid

0 favourites
  • 3 posts
From the Asset Store
Snap to visible grid - perfect solution for any game genre
  • Hi, I am making a completely shameless Terraria/Starbound ripoff (eventually it will be less shameless, I promise) and something I can't figure out is how to make the player only able to place blocks on a grid. I've figured out how to constrain their reach using a line of sight behavior on the player's hitbox object, but they can still be placed anywhere in space instead of snapping to the same 32x32 grid the tiles are on. I'm sure there is a solution here but I need your help to find it!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If you use a tilemap, and you should, then the tilemap has expressions to help you.

    PositionToTileX(positionx)

    PositionToTileY(positiony)

    Convert an X or Y layout co-ordinate in to the corresponding tile number in the tilemap. For example, this can be used to get the tile position under the mouse.

    SnapX(positionx)

    SnapY(positiony)

    Snap an X or Y layout co-ordinate to the nearest tile. This also returns a layout co-ordinate, but aligned to the nearest tile in the tilemap.

    TileToPositionX(indexx)

    TileToPositionY(indexy)

    Convert a tile position to layout co-ordinates. For example, this can be used to position a Sprite object on top of a given tile.

    Using snap.

    When a box is placed.

    Set the position of the box to

    x = tilemap.SnapX(box.X)

    y = tilemap.SnapY(box.Y)

    Using the other two.

    When a box is placed.

    Set the position of the box to

    x = TileToPositionX(PositionToTileX(box.X))

    y = TileToPositionY(PositionToTileY(box.Y))

    If you dont use a tilemap.

    When a box is placed.

    Set the position of the box to

    x = (round(box.X/32)) * 32

    y = (round(box.Y/32)) * 32

    But, that will not be exact.

  • That's very helpful, thank you!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)