- Home /
Place Gameobject within a specific tranform value 3d
Hi,
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.
exactly what is the question? how to move gameobjects to move to a grid point?
Yes, $$anonymous$$oving a gameobject to a grid point.
the elegant way is lost from me at this moment, but: float decimal = value - math.floor(value); if (decimal < 0.5) float x = math.floor(value) + 0.5; else float x = math.ceil(value); forgive me for how bad this solution is i shall spend some more time contemplating a better way
Answer by KKS21199 · Mar 05, 2017 at 10:58 AM
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
{
Vector3 newPosition;
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
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;
else
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;
else
newPosition.z = Mathf.Floor(newPosition.z) - 0.5f;
newPosition.y = 0.5f;
transform.position = newPosition;
}
}
}
Answer by tinglers · Mar 05, 2017 at 09:09 AM
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.
Answer by LiloE · Mar 04, 2017 at 10:44 PM
Can you be more specific about the value requirements? Can it be any multiple of 0.5 like -20, 3.5, 12, 75.5
?
Also, are the x,y,z values of the vector dependent on each other or not?
It shouldn't be in multiples. It more like the grid so if 0.5, 0, 0.5 is the first grid and the one next to it would be 1.5, 0, 0.5. https://i.gyazo.com/8d72ecec72a107f7d3867486e46cafe5.gif This is the quad grid I generated. So when user clicks I could get the specific quad and then put the game object in its position.
The x, y, z should be unique to each and (+ 1 in transform) on either x or z axis or both. It could start with 0, 0, 0 as well. I started with 0.5 so it aligns with unity's own grid.