S&box Wiki

Revision Difference

Camera#551236

<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. ⤶ ## 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 `[GameEvent.Client.Frame]` or `[GameEvent.Client.PostCamera]` (see [Event System](EventSystem) for more info) - or a method such as `FrameSimulate`:⤶ ⤶ <example>⤶ <description>Implementing a simple Camera to look around</description>⤶ <code>⤶ ...⤶ You can control the game camera by referencing the global clientside variable `Camera`. It is accessible and changeable anywhere in the game loop clientside. ⤶ You should your camera code in a method called every frame - like `FrameSimulate`.⤶ ⤶ ## Examples⤶ ⤶ ### Basic Camera⤶ ⤶ ```⤶ /// <summary> /// Get's the View as Angles used in <see cref="BuildInput"/> and <see cref="FrameSimulate"/> /// Get the View as Angles used in <see cref="BuildInput"/> and <see cref="FrameSimulate"/> /// </summary> [ClientInput]⤶ public Angles ViewAngles { get; set; }⤶ [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; ⤶ // Hide the player from the camera⤶ Camera.FirstPersonViewer = this; Camera.ZNear = 1f; Camera.ZFar = 5000.0f; } ...⤶ </code>⤶ </example>⤶ ⤶ <note>⤶ `Camera.FirstPersonViewer` will not hide the pawn if `EnableHideInFirstPerson = true` is not set. ⤶ </note>⤶ ⤶ # Using orthographic camera⤶ ```csharp⤶ public override void FrameSimulate( IClient cl )⤶ {⤶ base.FrameSimulate( cl );⤶ ⤶ //setting up orthographic camera⤶ Camera.Main.Ortho = true; ⤶ Camera.Main.OrthoWidth = 100f;⤶ Camera.Main.OrthoHeight = 100f;⤶ }⤶ ```⤶ ⤶ ### Orthographic Camera⤶ ``` public override void FrameSimulate( IClient cl )⤶ {⤶ base.FrameSimulate( cl );⤶ ⤶ Camera.Main.Ortho = true; ⤶ Camera.Main.OrthoWidth = 100f;⤶ Camera.Main.OrthoHeight = 100f;⤶ }⤶ ```⤶