S&box Wiki

Revision Difference

Camera#548292

<warning>CameraMode is now obsolete.</warning>⤶ <cat>Code.Camera</cat>⤶ <title>CameraMode</title>⤶ ⤶ # What is a CameraMode⤶ ⤶ CameraMode is an <page text="Entity Component">Entity Components</page> that can be put on any entity.⤶ It just allows us to say what Camera mode we want to make and or use on the player.⤶ such as ⤶ - First Person⤶ - Third Person⤶ - Free Cam⤶ ⤶ ⤶ The Default `Player` class that most games will be using already has CameraMode Property in there so all you need to do is set it.⤶ ⤶ The camera controls the position and rotation of the rendered scene on screen, imagine the camera as the players eyes, and where they are placed and rotated and such.⤶ ⤶ It also controls things like the field of view and whether or not to draw the player model.⤶ ⤶ ⤶ # Example⤶ ⤶ ```⤶ public class FirstPersonCamera : CameraMode⤶ {⤶ public override void Update()⤶ {⤶ var Pawn = Local.Pawn;⤶ ⤶ Position = Pawn.EyePosition;⤶ Rotation = Pawn.EyeRotation;⤶ ⤶ FieldOfView = 80;⤶ ⤶ Viewer = Pawn;⤶ }⤶ }⤶ ```⤶ ⤶ # Viewer⤶ ⤶ If you set Viewer it won't render that entity.⤶ ⤶ In first person modes you should set it to the entity you're inhabiting. In third person you should set it to null.⤶ ⤶ # Setting ⤶ ⤶ Setting the Camera is the same as setting a <page>Player Controller</page>.⤶ ⤶ ```⤶ public override void Respawn()⤶ {⤶ base.Respawn();⤶ ⤶ CameraMode = new FirstPersonCamera();⤶ }⤶ ```⤶ ⤶ Like the <page>Player Controller</page>, this property can be changed at any time and is replicated to the client.⤶ ⤶ # Getting⤶ ⤶ Getting our current camera from the player is almost as simple as how we set it.⤶ ```⤶ var CurrentCamera = Player.CameraMode;⤶ ```⤶ ⤶ However if you want to get the camera of any entity, you can also just do as so below.⤶ ⤶ ```⤶ var CurrentCamera = entity.Components.Get<CameraMode>();⤶ ```⤶