Back

Camera

Add Camera

Create a new empty entity: Create Entity -> Create Empty.

Add a CameraComponent to your entity.

You now have a camera on your entity.

Change Camera Zoom

There are two ways to change the camera zoom. One way is to change the zoom on the CameraComponent itself, the other way is to use the service CameraService. CameraService changes the currently active cameras zoom while CameraComponent zoom can set a specific camera's zoom.

Change with the CameraComponent

Create a new script component "ChangeZoom" and add it to the DefaultPlayer.

Add the event "KeyDownEvent" to the script.

Add the code inside the HandleKeyDownEvent function like below.


Event Handler:
    [service] (InputService)
    HandleKeyDownEvent( KeyDownEvent event )
    {
        if not isvalid(self.Entity.CameraComponent) then return end

        if event.key == KeyboardKey.Alpha1 then
            self.Entity.CameraComponent:SetZoomTo(100, 0.5)
            return
        end

        if event.key == KeyboardKey.Alpha2 then
            self.Entity.CameraComponent:SetZoomTo(125, 0.5)
            return
        end

        if event.key == KeyboardKey.Alpha3 then
            self.Entity.CameraComponent:SetZoomTo(150, 0.5)
            return
        end
    }
                    

Press Play and you can now change the player zoom by pressing the keys 1, 2 or 3.

Change with service _CameraService

Create a new script component "ChangeZoom" and add it to the DefaultPlayer.

Add the event "KeyDownEvent" to the script.

Add the code inside the HandleKeyDownEvent function like below.


Event Handler:
    [service] (InputService)
    HandleKeyDownEvent( KeyDownEvent event )
    {
        if event.key == KeyboardKey.Alpha1 then
            _CameraService:ZoomTo(100, 0.5)
            return
        end

        if event.key == KeyboardKey.Alpha2 then
            _CameraService:ZoomTo(125, 0.5)
            return
        end

        if event.key == KeyboardKey.Alpha3 then
            _CameraService:ZoomTo(150, 0.5)
            return
        end
    }
                    

Press Play and you can now change the player zoom by pressing the keys 1, 2 or 3.

Shake Camera

Create a new script component "ShakeCamera" and add it to the DefaultPlayer.

Add the event "KeyDownEvent" to the script.

Add the code inside the HandleKeyDownEvent function like below.


Event Handler:
    [service] (InputService)
    HandleKeyDownEvent( KeyDownEvent event )
    {
        if not isvalid(self.Entity.CameraComponent) then return end

        if event.key == KeyboardKey.Alpha1 then
            self.Entity.CameraComponent:ShakeCamera(0.5, 2)
            return
        end
    }
                    

Press Play and you will now shake the camera.

avatar