Hello!
I’ve tried everything but with no sucess. When you move the objects (link bellow) they should snap together or either snap into a grid in the plane.
https://photos.app.goo.gl/1EU6K2sh7cQLACp6A
I’ve tried fixed joint, collision detection, rigidbody freeze position, raycast but so far to no avail.
All i have to move the objects on the plane is
void OnMouseDrag()
{
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
Thanks in advance
Qriva
May 30, 2022, 3:57pm
2
You need to precise what do you mean by snap to grid. Do you mean scene grid? or runtime grid? or maybe freezing rotation on world axis?
let me try to explain . By using the plane with a set value of “squares” that when i move the objects they stay right on that “squares”. Like… aligned and neat, and not crashing into each other.
Not sure if it is possible with unity…
Qriva
May 30, 2022, 4:11pm
4
Everything is possible if you try hard enough
If you want to take into account other objects and irregular shapes then it will require more work, but to do simple snapping you need to find nearest “cell” of grid, something like this:
Mathf.RoundToInt(positonX / gridSizeX) * gridSizeX;
Qriva:
Everything is possible if you try hard enough
If you want to take into account other objects and irregular shapes then it will require more work, but to do simple snapping you need to find nearest “cell” of grid, something like this:
Mathf.RoundToInt(positonX / gridSizeX) * gridSizeX;
I found out that there is a grid component that i can add to the terrain. is that it?
EDIT: I’ve tried:
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
pos_move.x = (pos_move.x / terrainGrid.cellSize.x) * terrainGrid.cellSize.x;
pos_move.y = (pos_move.y / terrainGrid.cellSize.y) * terrainGrid.cellSize.y;
transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
but it does the same thing lol.
filipetakanap:
Hello!
I’ve tried everything but with no sucess. When you move the objects (link bellow) they should snap together or either snap into a grid in the plane.
https://photos.app.goo.gl/1EU6K2sh7cQLACp6A
I’ve tried fixed joint, collision detection, rigidbody freeze position, raycast but so far to no avail.
All i have to move the objects on the plane is
void OnMouseDrag()
{
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
Thanks in advance
Did you try any of the guides online? It seems like a common enough topic that there would be plenty of tutorials on how to do this stuff.
1 Like
Qriva
May 30, 2022, 5:22pm
7
I have no clue what is terrain grid.
Well, it does because you did x / y * y
and according to my math knowledge it still equals x
Compare yours to what I posted.
maybe i just don’t know what to search
Qriva:
I have no clue what is terrain grid.
Well, it does because you did x / y * y
and according to my math knowledge it still equals x
Compare yours to what I posted.
It is working for the Y Axis! the Z keeps doing the same, not sure why:
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
pos_move.x = Mathf.RoundToInt(pos_move.x / gridSizeX) * gridSizeX;
pos_move.z = Mathf.RoundToInt(pos_move.z / gridSizeZ) * gridSizeZ;
transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
EDIT: It should move around only on X and Z axis. not up and down on Y. It is just to move around the terrain
EDIT: Not sure what i have done, but it now moves, but only backwards xD
float distance_to_screen = Mathf.RoundToInt((float)(Camera.main.WorldToScreenPoint(gameObject.transform.position).z));
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
pos_move.x = Mathf.RoundToInt((pos_move.x / gridSizeX) * gridSizeX);
pos_move.z = Mathf.RoundToInt((pos_move.z / gridSizeZ) * gridSizeZ);
Debug.Log(pos_move.z);
gameObject.transform.position = new Vector3(pos_move.x, transform.position.y, pos_move.z);
i found out that for it to work i need to push the camera upwards and rotate it downwards… If you have better sugestions, please, say so.
Thanks