I’m making a level creator, and I’ve finished making the rotation tool. Now I want to implement rotation snapping; this seems like it should be easy but I can’t find a reliable way to do it.
I’m rotating objects by multiplying their rotation with the add rotation, and I’m getting the add rotation by seeing how much the controller moved between this frame and the last frame (this is a VR project).
void RotateObject()
{
float delta = 0;
directionGizmos.SetActive(true);
Vector3 add = Vector3.zero;
switch ( currentAxis )
{
case 0: // x
delta = -(currentController.localPosition.y - dragStartPos.y);
add = new Vector3(delta, 0, 0);
directionGizmos.transform.localRotation = Quaternion.Euler(90, -90, 0);
break;
case 1: // y
delta = currentController.localPosition.x - dragStartPos.x;
add = new Vector3(0, delta, 0);
directionGizmos.transform.localRotation = Quaternion.Euler(0, -90, 0);
break;
case 2: // z
delta = -(currentController.localPosition.y - dragStartPos.y);
add = new Vector3(0, 0, delta);
directionGizmos.transform.localRotation = Quaternion.Euler(90, -90, 0);
break;
}
add *= rotateSpeedMultiplier;
if ( ToolSelector.IsLocal )
{
Selector.instance.currentSelected.transform.localRotation *= Quaternion.Euler(add);
}
else
{
Selector.instance.currentSelected.transform.rotation *= Quaternion.Euler(add);
rotateGizmos.transform.rotation *= Quaternion.Euler(add);
}
dragStartPos = currentController.localPosition;
}
Any help is appreciated, thanks!