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.
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
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 ();
}