- Home /
Align objects to differently sized grids
I am trying to make it so that objects can align to a grid on my 2D game. In my game you can adjust the scale of objects, so just using mathf.round wasn't enough as objects would be put in the center of each grid square and they would be spaced apart if the scale was below 1. The way I fixed this was doing this:
Vector3 currentPos = gameObject.transform.position;
gameObject.transform.position = new Vector2(Mathf.Round(currentPos.x * (1/scale)) / scale,Mathf.Round(currentPos.y * (1/scale)) / scale;
But, this didn't work for me the way I wanted to because objects then wouldn't align with each other correctly.
Instead of the grids they were aligning to just being the original grid but with differently sized grid squares, they seem to have some offset. This has been bothering me for a while and I haven't been able to find a situation. How do I fix this?
Answer by OwlTeaGames · Jun 14, 2020 at 08:08 PM
Have you tried using Grid Layout? It's a component you add to the parent object of whatever you're trying to organize. I don't know if it'll help you, but it helped me!
Your answer
Follow this Question
Related Questions
unity Vector2 problem 1 Answer
Converting direction to Vector2 2 Answers
Is there a more efficient way to pass two integers via "SendMessage" than using a Vector2 Struct? 1 Answer
Rotating A Vector2 Input Into A Vector3 1 Answer
Using vector2 (transform.position.x, transform.position.y -= 1) error 2 Answers