I am trying to place a game object wherever the user chooses within the game (3d) but it has to has a specific transform value (0.5, 0, 0.5) (0.5, 0, -0.5) (-0.5, 0, 0.5) (-0.5, 0, -0.5).
I tried generating quads during the start of the game so I could have a template and use OnMouseOver function to decide where to place the gameobject. But the quad’s degrade performance as soon as I start increasing the number of quads within the scene (at about 200x200 FPS starts falling gradually and at 500x500 FPS is only about 5-10).
So I decided to lock the position when the user places the gameobject. So if they touch near (0, 0, 0.1), the gameobject should be moved to (0.5, 0, 0.5).
Any ideas on how I could do the logic? The ground would be a mesh and will have a flat surface.
if your planes are 1 big then simply
x = math.floor(mouseX) + 0.5 and
z = math.floor(mouseZ) + 0.5 should do the trick. This calculation won’t necessarily make the value a unique one so for that you’d have to check with the quad on the resulting position.
Can you be more specific about the value requirements? Can it be any multiple of 0.5
-20, 3.5, 12, 75.5 ?
Also, are the x,y,z values of the vector dependent on each other or not?
This script works perfectly. Thanks to @tinglers for the idea. This will lock the gameobject to the grid. Anybody looking for the same solution can use the function below and extend the function to check if the points are unique.
void placeItem() //placeItem wherever mouse clicked
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
newPosition = hit.point;
float xPos = Mathf.Abs(newPosition.x - Mathf.Floor(newPosition.x));
if (xPos > 0.5f)
newPosition.x = Mathf.Floor(newPosition.x) + 0.5f;
newPosition.x = Mathf.Floor(newPosition.x) - 0.5f;
float zPos = Mathf.Abs(newPosition.z - Mathf.Floor(newPosition.z));
if (zPos > 0.5f)
newPosition.z = Mathf.Floor(newPosition.z) + 0.5f;
newPosition.z = Mathf.Floor(newPosition.z) - 0.5f;
newPosition.y = 0.5f;
transform.position = newPosition;