Skip to content

Instantly share code, notes, and snippets.

@Spartan322
Last active July 4, 2020 21:41
Show Gist options
  • Save Spartan322/08e56a79c6a7f145168eb2c782274a77 to your computer and use it in GitHub Desktop.
Save Spartan322/08e56a79c6a7f145168eb2c782274a77 to your computer and use it in GitHub Desktop.
// in physics process of the camera
// vpSize is the viewport size
// MouseWatcher.MouseOriginLocal is the last local mouse position tracked from one of my global classes
// if statement moves the camera on zoom in and not zoom out (to invert behavior make it more then, or remove the if if you want zoom in and out to move the camera
// and remove both statements if you don't want it to move)
var nzoom = Zoom.LinearInterpolate(Vector2.One * zoomFactor, ZoomSpeed * delta);
if (nzoom < Zoom)
Position = Position + (-0.5f * vpSize + MouseWatcher.MouseOriginLocal) * (Zoom - nzoom);
// in unhandled input of the camera (so a focused control element or other gui input can override it first so you don't accidentally zoom)
// zoomDirection basically boils down into tracking the zoom out and zoom in actions, its just a bit more sophisticated and easier to manage at the top of the file
// ZoomSpeed is the speed of the zoom step
// ZoomStep is the inverted divisor for creating steps between the zooms (so the higher the number, the more the steps, the lower the number, the less the steps)
// ZoomLimits is a Vec2 that determines the min (x) and max (y) of the zoom
var zoomDelta = zoomDirection.GetFloat(InputZoomDirection.Outward) - zoomDirection.GetFloat(InputZoomDirection.Inward);
if (Mathf.Abs(zoomDelta) >= 1)
zoomFactor = Mathf.Clamp(zoomFactor + zoomDelta / ZoomStep * ZoomSpeed, ZoomLimits.x, ZoomLimits.y);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment