S&box Wiki

Revision Difference

Camera#546579

<cat>Code.Camera</cat> <title>Camera</title>⤶ # What is a Camera⤶ ⤶ The camera controls the position and rotation of the rendered scene. <title>CameraMode</title>⤶ # What is a CameraMode⤶ ⤶ CameraMode is an <page>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 : Camera⤶ public class FirstPersonCamera : CameraMode⤶ { public override void Update() { var Pawn = Local.Pawn; Position = Pawn.EyePos; Rotation = Pawn.EyeRot; 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(); Camera = new FirstPersonCamera(); 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 is almost as simple as getting it, from the player.⤶ ``` ⤶ Like the <page>Player Controller</page>, this property can be changed at any time and is replicated to the client.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>();⤶ ```⤶