Hello, so i got raycast working and instantiating a object on the place where i click with the mouse,
Now i would like to know how i could make it snap INGAME (so not the editor itself) onto a ‘‘grid’’ which you can/cant see if its needed. I’d love to know how this works and how one can achieve it.
Thanks in advance!
If the unit of your grid is of size 1 squares, you can just round the position numbers. This is one way I could think of in short time. There probably are better more effective methods.
Vector3 pos = /*clicked point*/;
float RoundNearest(float amt){
if(Mathf.Ceil(amt) - amt > 0.5f){
return Mathf.Ceil(amt);
} else {
return Mathf.Floor(amt);
}
}
pos = new Vector3(RoundNearest(pos.x), RoundNearest(pos.y), RoundNearest(pos.z));
Instantiate(/*object*/, pos, /*rotation*/);
Well, i dont have a grid yet