Inspector Camera Controller
Summary#
The InspectorCameraController orbits a camera around a Target location on a sphere defined by Yaw and Pitch angles, always pointing at the target. This is useful for "model viewer" or "inspector" style cameras that allow a user to inspect an object from any angle.
- Adjusting
Yawrotates the camera around the target; - Adjusting
Pitchtilts the camera up or down relative to the target; - Adjusting
Distancemoves the camera toward or away from the target.
Example Usage#
// One time setup:
var controller = camera.CreateController<InspectorCameraController>(); // (1)!
controller.Target = Location.Origin; // (2)!
controller.WorldUp = Direction.Up; // (3)!
// Per-frame:
controller.AdjustAllViaDefaultControls(input.KeyboardAndMouse, deltaTime); // (4)!
controller.AdjustAllViaDefaultControls(input.GameControllersCombined, deltaTime); // (5)!
controller.Progress(deltaTime); // (6)!
- This creates the controller, attached to the given
camera. - This sets the location the camera will orbit around (and always look at).
-
This sets which way is "up" — the axis the camera yaws around, and the reference for what "up" means when tilting via
Pitch. -
This manipulates the camera according to the default keyboard and mouse scheme. The yaw/pitch/distance properties will change according to any registered user inputs for this frame.
You can replace this with more specific control code if desired (see below); or remove it entirely if you do not wish to allow user keyboard/mouse input control.
-
This manipulates the camera according to the default game controller scheme for all game controllers combined. The yaw/pitch/distance properties will change according to any registered user inputs for this frame.
You can replace this with more specific control code if desired (see below); or remove it entirely if you do not wish to allow user gamepad input control.
-
Calling
Progress()once per frame is required on all camera controllers in order for them to actually alter their targetCamera's parameters.
Properties#
Per-Frame Targets#
-
Yaw -
Sets how far around the target the camera has rotated, around the
WorldUpaxis.Increasing this value turns the camera clockwise around the target; decreasing turns anticlockwise.
Defaults to
0°. -
Pitch -
Sets how high or low the camera is tilted relative to the target.
A value of
0°places the camera level with the target on the horizontal plane; positive values tilt upward, negative values tilt downward.By default, this value is automatically clamped to ±90° during
Progress()so that the camera never flips upside-down. SetAllowUpsideDownFliptotrueto disable this behavior.Defaults to
0°. -
Distance -
Sets how far from the
Targetthe camera is.This value will be clamped between
MinDistanceandMaxDistancewhen either are non-null.Defaults to
0.6f.
Configuration#
-
Target -
The location in the world that the camera is orbiting around, and always looking at.
Defaults to
Location.Origin. -
WorldUp -
The world's "up" direction. The camera yaws around this axis, and
Pitchis interpreted relative to it.Defaults to
Direction.Up. -
AllowUpsideDownFlip -
When
false(the default),Pitchis automatically clamped to ±90° each frame so the camera never tilts past straight-up or straight-down.When
true, no such clamp is applied — the camera is free to flip upside-down.Defaults to
false.
-
MinDistance -
The minimum permitted value for
Distance. Can benullto remove the lower bound.Defaults to
0.6f. -
MaxDistance -
The maximum permitted value for
Distance. Can benullto remove the upper bound.Defaults to
2f.
Reacting to Input#
As camera controllers are often meant to be affected by user input, there are some convenience methods supplied for controlling the primary per-frame target properties:
Adjusting Pitch#
Keyboard / Mouse#
-
AdjustPitchViaMouseCursor(...) -
Adjusts
Pitchaccording to the captured mouse cursor movement for this frame.The
axissets which cursor movement direction will be used (defaults toY, e.g. up/down).The
adjustmentPerPixelvalue is the angle to add toPitchfor each pixel moved according to the givenaxis. If null,DefaultPitchSensitivityMouseCursorwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustPitchViaMouseWheel(...) -
Adjusts
Pitchaccording to the captured mouse wheel movement for this frame.The
adjustmentPerWheelIncrementvalue is the angle to add toPitchfor each scroll increment on the mouse wheel. If null,DefaultPitchSensitivityMouseWheelwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustPitchViaKeyPress(...) -
Adjusts
Pitchaccording to whether a certain key is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
keyToTestForis the key that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two keys in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the angle to add toPitchfor each second this key is depressed. If null,DefaultPitchSensitivityKeyOrButtonPresswill be used.
Gamepad#
-
AdjustPitchViaControllerStick(...) -
Adjusts
Pitchaccording to the captured controller stick position for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
axissets which stick movement direction will be used (defaults toY, e.g. up/down).The
maxAdjustmentPerSecvalue is the angle to add toPitchwhen the stick is fully displaced along the givenaxis. If null,DefaultPitchSensitivityControllerStickwill be used.If
useLeftStickis true, the left controller stick will be measured; otherwise the right stick will be measured. Defaults tofalse.If
invertStickControlistrue, the calculated adjustment will be reversed. -
AdjustPitchViaControllerTriggers(...) -
Adjusts
Pitchaccording to the captured controller trigger positions for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
maxAdjustmentPerSecvalue is the angle to add toPitchwhen the trigger is fully displaced. If null,DefaultPitchSensitivityControllerTriggerwill be used.If
leftTriggerPitchesUpis true, the left trigger will pitch up and the right trigger pitch down; otherwise these directions will be reversed. Defaults totrue. -
AdjustPitchViaButtonPress(...) -
Adjusts
Pitchaccording to whether a certain button is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
buttonToTestForis the button that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two buttons in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the angle to add toPitchfor each second this button is depressed. If null,DefaultPitchSensitivityKeyOrButtonPresswill be used.
Other#
-
AdjustPitch(...) -
Adjusts
Pitchaccording to the given turn rate (adjustmentPerSec) and time step (deltaTime).This method does not inspect any user input data but is provided as a convenience for building custom per-frame control code.
Adjusting Yaw#
Keyboard / Mouse#
-
AdjustYawViaMouseCursor(...) -
Adjusts
Yawaccording to the captured mouse cursor movement for this frame.The
axissets which cursor movement direction will be used (defaults toX, e.g. left/right).The
adjustmentPerPixelvalue is the angle to add toYawfor each pixel moved according to the givenaxis. If null,DefaultYawSensitivityMouseCursorwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustYawViaMouseWheel(...) -
Adjusts
Yawaccording to the captured mouse wheel movement for this frame.The
adjustmentPerWheelIncrementvalue is the angle to add toYawfor each scroll increment on the mouse wheel. If null,DefaultYawSensitivityMouseWheelwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustYawViaKeyPress(...) -
Adjusts
Yawaccording to whether a certain key is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
keyToTestForis the key that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two keys in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the angle to add toYawfor each second this key is depressed. If null,DefaultYawSensitivityKeyOrButtonPresswill be used.
Gamepad#
-
AdjustYawViaControllerStick(...) -
Adjusts
Yawaccording to the captured controller stick position for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
axissets which stick movement direction will be used (defaults toX, e.g. left/right).The
maxAdjustmentPerSecvalue is the angle to add toYawwhen the stick is fully displaced along the givenaxis. If null,DefaultYawSensitivityControllerStickwill be used.If
useLeftStickis true, the left controller stick will be measured; otherwise the right stick will be measured. Defaults tofalse.If
invertStickControlistrue, the calculated adjustment will be reversed. -
AdjustYawViaControllerTriggers(...) -
Adjusts
Yawaccording to the captured controller trigger positions for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
maxAdjustmentPerSecvalue is the angle to add toYawwhen the trigger is fully displaced. If null,DefaultYawSensitivityControllerTriggerwill be used.If
leftTriggerYawsClockwiseis true, the left trigger will yaw anticlockwise and the right trigger yaw clockwise; otherwise these directions will be reversed. Defaults totrue. -
AdjustYawViaButtonPress(...) -
Adjusts
Yawaccording to whether a certain button is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
buttonToTestForis the button that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two buttons in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the angle to add toYawfor each second this button is depressed. If null,DefaultYawSensitivityKeyOrButtonPresswill be used.
Other#
-
AdjustYaw(...) -
Adjusts
Yawaccording to the given turn rate (adjustmentPerSec) and time step (deltaTime).This method does not inspect any user input data but is provided as a convenience for building custom per-frame control code.
Adjusting Distance#
Keyboard / Mouse#
-
AdjustDistanceViaMouseCursor(...) -
Adjusts
Distanceaccording to the captured mouse cursor movement for this frame.The
axissets which cursor movement direction will be used (defaults toY, e.g. up/down).The
adjustmentPerPixelvalue is the amount to add toDistancefor each pixel moved according to the givenaxis. If null,DefaultDistanceSensitivityMouseCursorwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustDistanceViaMouseWheel(...) -
Adjusts
Distanceaccording to the captured mouse wheel movement for this frame.The
adjustmentPerWheelIncrementvalue is the amount to add toDistancefor each scroll increment on the mouse wheel. If null,DefaultDistanceSensitivityMouseWheelwill be used.If
invertMouseControlistrue, the calculated adjustment will be reversed. -
AdjustDistanceViaKeyPress(...) -
Adjusts
Distanceaccording to whether a certain key is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
keyToTestForis the key that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two keys in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the amount to add toDistancefor each second this key is depressed. If null,DefaultDistanceSensitivityKeyOrButtonPresswill be used.
Gamepad#
-
AdjustDistanceViaControllerStick(...) -
Adjusts
Distanceaccording to the captured controller stick position for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
axissets which stick movement direction will be used (defaults toY, e.g. up/down).The
maxAdjustmentPerSecvalue is the amount to add toDistancewhen the stick is fully displaced along the givenaxis. If null,DefaultDistanceSensitivityControllerStickwill be used.If
useLeftStickis true, the left controller stick will be measured; otherwise the right stick will be measured. Defaults tofalse.If
invertStickControlistrue, the calculated adjustment will be reversed. -
AdjustDistanceViaControllerTriggers(...) -
Adjusts
Distanceaccording to the captured controller trigger positions for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
maxAdjustmentPerSecvalue is the amount to add toDistancewhen the trigger is fully displaced. If null,DefaultDistanceSensitivityControllerTriggerwill be used.If
leftTriggerIncreasesDistanceis true, the left trigger will increase distance from the target and the right trigger decrease it; otherwise these directions will be reversed. Defaults totrue. -
AdjustDistanceViaButtonPress(...) -
Adjusts
Distanceaccording to whether a certain button is depressed for this frame.The
deltaTimevalue is expected to be the time in seconds of this frame iteration.The
buttonToTestForis the button that, when pressed, will adjust this property.If
reverseistrue, the calculated adjustment will be reversed. Defaults tofalse. This parameter lets you specify two buttons in a pair that mirror each other by invoking this method twice (once withreverseasfalseand once withreverseastrue).The
adjustmentPerSecvalue is the amount to add toDistancefor each second this button is depressed. If null,DefaultDistanceSensitivityKeyOrButtonPresswill be used.
Other#
-
AdjustDistance(...) -
Adjusts
Distanceaccording to the given rate (adjustmentPerSec) and time step (deltaTime).This method does not inspect any user input data but is provided as a convenience for building custom per-frame control code.
Default Controls#
The following snippets show the implementation of AdjustAllViaDefaultControls(...) for keyboard/mouse and gamepad respectively:
// AdjustAllViaDefaultControls(input.KeyboardAndMouse, deltaTime):
AdjustPitchViaMouseCursor(input, pitchAdjustmentPerPixel, invertMouseControl: invertPitchControl);
AdjustYawViaMouseCursor(input, yawAdjustmentPerPixel, invertMouseControl: invertYawControl);
AdjustDistanceViaMouseWheel(input, distanceAdjustmentPerWheelIncrement, invertMouseControl: invertDistanceControl);
// AdjustAllViaDefaultControls(input.GameControllersCombined, deltaTime):
AdjustPitchViaControllerStick(input, deltaTime, maxPitchAdjustmentPerSec, invertStickControl: invertPitchControl);
AdjustYawViaControllerStick(input, deltaTime, maxYawAdjustmentPerSec, invertStickControl: invertYawControl);
AdjustDistanceViaControllerTriggers(input, deltaTime, maxDistanceAdjustmentPerSec, leftTriggerIncreasesDistance: !invertDistanceControl);
Smoothing#
Smoothing changes how quickly the controller adjusts the camera to match the current target properties.
The Yaw, Pitch, and Distance target properties can have smoothing applied. Note that Yaw and Pitch share a single RotationSmoothingStrength setting — they cannot be smoothed independently of one another.
// Set properties' smoothing individually:
controller.RotationSmoothingStrength = Strength.VeryMild; // Applies to both Yaw and Pitch
controller.DistanceSmoothingStrength = Strength.VeryMild;
// Set all properties' smoothing simutaneously:
controller.SetGlobalSmoothing(Strength.VeryMild);
The default smoothing for all properties is VeryMild. You can choose from VeryMild, Mild, Moderate, Strong, VeryStrong, or None.
- Smoothing makes the camera feel more 'real' or physical.
- Higher strengths increase this feeling but also increase the latency between setting a target value and the camera actually meeting that target.
- Setting the smoothing to
Nonedisables smoothing entirely. This means the camera will always be updated to meet exactly the target value of each property on each frame; reducing latency to 0 but making the camera feel less physical.
Custom Smoothing Values
If the enum-based approach is not specific enough for your needs, every property can instead have a custom smoothing strength applied via a method named like "SetCustom[...]SmoothingStrength".
This method takes a single float parameter that indicates the half-life of decay between the current value of a property and its target value.
For example, if the current value of X is 50 and the target value of X is 100, a half-life of 1f would move X to 75 after one second, and then 87.5 after the next second, and so-on.
Advanced: Smoothing is implemented via critically-damped spring. The smoothingHalfLife parameter is translated to become the Ω of the spring equation via the formula Ω = 1.6783469f / smoothingHalfLife.