This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hi guys I wanted to know how I could align an object to a grid or to the same godot grid, what I want to achieve is to make a drag and drop with automatic alignment but I don't know anything about grid, the only and most basic thing that I managed to do was create the object at mouse position

Godot version 3.2.3
in Engine by (17 points)

2 Answers

+2 votes
Best answer

You can use something like this to get a position snapped to a grid:

var CELL_SIZE = 32 # or whatever

func position_snapped(pos:Vector2):
    return (pos / CELL_SIZE).floor() * CELL_SIZE

If you are using a TileMap you can use its maptoworld and worldtomap functions to transform world coordinates into grid positions.

by (194 points)
selected by
+1 vote

If you have a Vector2 position for your object, you can use the built-in Vector2 function snapped(Vector2 by):

position = position.snapped(Vector2.ONE * grid_size)

replace grid_size with whatever grid size you are using.

From the docs:

by (36 points)

yeah! thanks to all it served me

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.