S&box Wiki

Revision Difference

Camera#548926

<cat>Code.Camera</cat> <title>Camera</title> # Introduction You can control the game camera by referencing the global clientside variable `Camera`. It is accessible and changeable anywhere in the game loop clientside. ⤶ # Example usage⤶ ⤶ ## Help, I'm Upgrading⤶ If you are currently transitioning from the old way of doing things to the new way, here are some pointers⤶ ⤶ - `FrameSimulate( Client cl )` changed to `FrameSimulate( IClient cl )`⤶ ⤶ - `Game.UserPreference.FieldOfView` changed to `Game.Preferences.FieldOfView`⤶ ⤶ - `EyePosition` changed to `AimRay.Position`⤶ ⤶ ⤶ # Usage⤶ It is recommended to put your camera code in an event called every frame such as `[Event.Client.Frame]` or `[Event.Client.PostCamera]` (see [Event System](EventSystem) for more info) - or a method such as `FrameSimulate`: ⤶ ```⤶ public override void FrameSimulate( Client cl )⤶ ⤶ <example>⤶ <description>Implementing a simple Camera to look around</description>⤶ <code>⤶ ...⤶ /// <summary>⤶ /// Get's the View as Angles used in <see cref="BuildInput"/> and <see cref="FrameSimulate"/> ⤶ /// </summary>⤶ [ClientInput]⤶ public Angles ViewAngles { get; set; }⤶ ⤶ /// <summary>⤶ /// Allows to query the input and perform operations with it⤶ /// </summary>⤶ public override void BuildInput()⤶ {⤶ InputDirection = Input.AnalogMove;⤶ ⤶ var look = Input.AnalogLook;⤶ ⤶ var viewAngles = ViewAngles;⤶ viewAngles += look;⤶ ViewAngles = viewAngles.Normal;⤶ }⤶ ⤶ /// <summary>⤶ /// Called every frame on the client⤶ /// </summary>⤶ public override void FrameSimulate( IClient cl )⤶ { base.FrameSimulate( cl );⤶ ⤶ // Update rotation every frame, to keep things smooth⤶ Camera.Rotation = ViewAngles.ToRotation(); Camera.Position = EyePosition; Camera.FieldOfView = Game.UserPreference.FieldOfView; Camera.Position = AimRay.Position; Camera.FieldOfView = Game.Preferences.FieldOfView; Camera.FirstPersonViewer = this; Camera.ZNear = 1f; Camera.ZFar = 5000.0f; } ```⤶ ⤶ **Note:** `Camera.FirstPersonViewer` will not hide the pawn if `EnableHideInFirstPerson = true` is not set. ...⤶ </code>⤶ </example>⤶ ⤶ <note>⤶ `Camera.FirstPersonViewer` will not hide the pawn if `EnableHideInFirstPerson = true` is not set. ⤶ </note>