S&box Wiki

Camera

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.

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 for more info) - or a method such as FrameSimulate:

Example

Implementing a simple Camera to look around

... /// <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 = AimRay.Position; Camera.FieldOfView = Game.Preferences.FieldOfView; Camera.FirstPersonViewer = this; Camera.ZNear = 1f; Camera.ZFar = 5000.0f; } ...
Camera.FirstPersonViewer will not hide the pawn if EnableHideInFirstPerson = true is not set.