- Home /
Rounding Errors when snapping to grid
I am making a building game which has freeform and snap to grid build options. When using freeform, the below code works perfectly, but when useGridSnap is true, the rounding used in this example causes problems:
void PositionGhost(){
Vector3 targetPos = rayHitPoint;
if (useGridSnap) {
/*Bug: This rounding causes an error which sometimes makes the block
* position itself incorrectly when placing on grid */
ghostTrans.position = new Vector3 (Mathf.Round (targetPos.x),
Mathf.Round (targetPos.y),
Mathf.Round (targetPos.z));
} else {
ghostTrans.position = rayHitPoint;
}
ghostTrans.up-=(ghostTrans.up-rayHitNormal);
ghostTrans.Translate (new Vector3(0,buildOffset,0));
CheckBuildValid ();
}
Specifically, the snap to grid works perfectly when placing on top of an object, but is thrown off when trying to place on the side of an object. Freeform offsets correctly whether on top or side of object.
I need a way to improve the accuracy of the rounding, maybe by taking into account the plane normal that is being hit and only returning the rounding of the X & Z relative to the plane? Although I can see that still giving me issues.
Any advice would be appreciated.
Here is a quick video showing the issue - https://youtu.be/c3aTqy4Ii2c
Answer by SoulRider · Aug 21, 2015 at 06:24 PM
Thanks for the help with this issue, I have now come up with a working solution, which I'll post back as I always get infuriated when someone fixes something themselves and doesn't post how.
Thanks for pointing me in the object position direction @Adam Halley-Prinable
I simply hit an object, get it's plane normal and it's position, then shift the ghost object by 1 grid position in it's new 'up' direction. If I hit the the terrain I just use the grid as normal.
This should work with any object, whether origin in middle, or at bottom, however, it will cause issues if trying to combine the two.
Here is the new working function for this issue:
void PositionGhost(){
if (useGridSnap) {
if (hitObject) {
ghostTrans.position = hitObject.transform.position;
ghostTrans.up-=(ghostTrans.up-rayHitNormal);
ghostTrans.Translate (new Vector3(0,1,0));
}
else {
ghostTrans.position = new Vector3 (Mathf.Round rayHitPoint.x),
Mathf.Round (rayHitPoint.y),
Mathf.Round (rayHitPoint.z));
ghostTrans.up-=(ghostTrans.up-rayHitNormal);
ghostTrans.Translate (new Vector3(0,buildOffset,0));
}
} else {
ghostTrans.position = rayHitPoint;
ghostTrans.up-=(ghostTrans.up-rayHitNormal);
ghostTrans.Translate (new Vector3(0,buildOffset,0));
}
CheckBuildValid ();
}
Answer by Adam-Halley-Prinable · Aug 20, 2015 at 01:49 PM
I would make my own rounding function and custom-make it for this purpose. Round every number in a vector3 to the nearest multiple of a grid space width
It does that already. The grid space width is 1, default unity. Round automatically makes everything an int, so a multiple of 1.
Could it be that it's snapping to world space and not snapping to the position of that box +/- 1?
also, shouldn't targetPos be the position of the object raycasted to, rather than the position where the raycast landed?
The target position needs to be where I want to place the object, so this is the point on the collider where my raycast hits. I use ClosestPointOnBounds for this. This works for the freeform mode, but you are right, it might be causing an issue with the Snap $$anonymous$$ode. When I adjust for the origin in the centre of the objects. This is where the issue seems to be occurring.
I doubled the buildOffset, and it solves the issue of offset placement using snap to grid. However, I am left with my blocks floating above the ground ins$$anonymous$$d of placing on it. It's a semi-solution, well, once I resolve the floating above ground issues.
I played with using the object I think the real problem is the origin being in the centre of the block. If it was at the bottom, I believe I wouldn't have this issue..
Your answer
Follow this Question
Related Questions
Is there a toggle for snap to grid? 10 Answers
Gizmo won't stop snapping to grid. 1 Answer
Snap object to grid 1 Answer
Check if object is on grid 1 Answer
How to drag gameobject (only x y) snapped to a grid? 2 Answers