Class Basic3DFollowCameraController
A third-person follow camera: it hangs behind and above a target entity, keeps it in view as it moves, and lets the player swing round it with the mouse. Dragging with OrbitButton orbits, the mouse wheel moves closer or further, and 'H' returns to the starting view. With an ObstacleTest the camera comes forward rather than pass through a wall between it and the target.
[Display("Follow Camera Controller", null)]
[ComponentCategory("Camera")]
public class Basic3DFollowCameraController : SyncScript, IIdentifiable, ICollectorHolder
- Inheritance
-
Basic3DFollowCameraController
- Implements
- Inherited Members
- Extension Methods
Remarks
Attach it to an entity with a CameraComponent and set Target. The camera looks at the target's position plus TargetOffset, the pivot, which follows the target with a short lag set by FollowSmoothing so the view does not jolt with every step. The entity's starting position relative to the pivot gives the first distance and angles; an entity sitting on the pivot starts at Distance, behind the target and a little above.
The controller knows nothing of physics. A game that wants the camera to stay out of walls gives it an ObstacleTest, a raycast from the pivot to the wanted position in whatever engine it uses, and the camera stops ObstacleMargin short of the first hit.
Pitch is the camera's own: negative looks down from above the target, and the clamps MinPitch and MaxPitch keep it from flipping over the top or sinking through the floor. Both angles are measured against Up, the world's up unless the game says otherwise: a character walking round a planet sets it to the direction away from the centre every frame, and the camera orbits and pitches over the surface. Forward and Right are the matching ground directions, what a character that walks "forward" wants to read. The 'F2' help section is shared with the other controllers through TextOverlay.
Properties
Distance
Gets or sets the distance the camera starts at when its entity begins on the pivot, in world units. Defaults to 6.
public float Distance { get; set; }
Property Value
FollowSmoothing
Gets or sets how long the pivot takes to close most of the gap to the target, in seconds: the lag that
smooths a character's steps out of the view. 0 follows exactly. Defaults to 0.12.
public float FollowSmoothing { get; set; }
Property Value
Forward
Gets the direction the camera looks along the ground: the look direction with the pitch taken out. A character that walks "forward" walks this way.
public Vector3 Forward { get; }
Property Value
HelpCollapsed
Gets or sets whether the help section starts collapsed. Defaults to true.
public bool HelpCollapsed { get; set; }
Property Value
HelpToggleKey
Gets or sets the key that collapses and expands the help section. Defaults to F2.
public Keys HelpToggleKey { get; set; }
Property Value
MaxDistance
Gets or sets the furthest the camera can go, in world units. Defaults to 30.
public float MaxDistance { get; set; }
Property Value
MaxPitch
Gets or sets the highest pitch in radians: how far below the pivot the camera may sink. Defaults to about 20°.
public float MaxPitch { get; set; }
Property Value
MinDistance
Gets or sets the closest the camera can come, in world units. Defaults to 1.
public float MinDistance { get; set; }
Property Value
MinPitch
Gets or sets the lowest pitch in radians: how far above the target the camera may climb. Defaults to about −80°.
public float MinPitch { get; set; }
Property Value
ObstacleMargin
Gets or sets how far short of an obstacle the camera stops, in world units. Defaults to 0.3.
public float ObstacleMargin { get; set; }
Property Value
ObstacleReturnSmoothing
Gets or sets how long the camera takes to ease most of the way back out once an obstacle has cleared, in
seconds. Coming in is always immediate, so the camera never clips; going back out at once would be a jump.
0 returns at once. Defaults to 0.3.
public float ObstacleReturnSmoothing { get; set; }
Property Value
ObstacleTest
Gets or sets the test for walls between the pivot and the camera, or null to pass through everything.
public CameraObstacleTest? ObstacleTest { get; set; }
Property Value
OrbitButton
Gets or sets the mouse button that orbits while held. Defaults to the right button, leaving the left one to the game.
public MouseButton OrbitButton { get; set; }
Property Value
OrbitSensitivity
Gets or sets how far a drag orbits, in radians per full window of mouse travel. Defaults to 4.
public float OrbitSensitivity { get; set; }
Property Value
Pitch
Gets or sets the pitch the camera starts at when its entity begins on the pivot, in radians. Defaults to about −20°, looking down a little.
public float Pitch { get; set; }
Property Value
Pivot
Gets the point the camera is looking at: the target plus its offset, after the follow lag.
public Vector3 Pivot { get; }
Property Value
Right
Gets the direction to the camera's right along the ground.
public Vector3 Right { get; }
Property Value
SpeedFactor
Gets or sets the multiplier applied to zoom while a shift key is held. Defaults to 5.
public float SpeedFactor { get; set; }
Property Value
Target
Gets or sets the entity the camera follows. With none set the camera stays where it is.
public Entity? Target { get; set; }
Property Value
TargetOffset
Gets or sets the point the camera looks at, relative to the target's position: a character's chest rather than its feet. Defaults to 1.5 up.
public Vector3 TargetOffset { get; set; }
Property Value
Up
Gets or sets the direction the camera treats as up, which the yaw turns round and the pitch leans away from. Defaults to the world's up. On a planet, set it to the direction from the centre to the target every frame.
public Vector3 Up { get; set; }
Property Value
Yaw
Gets the camera's yaw in radians: the angle of its ground direction round Up, measured from the world's +Z laid into the ground. The direction itself is kept from frame to frame and carried along as the up changes, so a walk over a planet never flips the view; the angle is only its reading.
public float Yaw { get; }
Property Value
ZoomStep
Gets or sets the fraction the distance changes per mouse-wheel notch. Defaults to 0.1 (10 %).
public float ZoomStep { get; set; }
Property Value
Methods
Start()
Reads the starting pose from the entity's position relative to the pivot, or takes the defaults when the entity sits on it, and registers the shared help section.
public override void Start()
Update()
Moves the pivot after the target, processes orbit, zoom and reset input, and places the camera, pulled in from any obstacle.
public override void Update()