Categories
Advanced Tutorials

The relationship between Player and Default camera

Preface

In most 3D game engines the concept of a camera is quite common. Cameras are the devices that capture and display the world to the player.

Crayta provides this mechanism: a default camera with a number of customization options to easily achieve your desired gameplay camera and the option to spawn any number of secondary cameras to be further used.

In this tutorial we will explore the relationship between the default camera and the player, and also how to create and use additional in-game cameras.

If you are coming from a Unity background you will be familiar with camera game objects.

What’s different in Crayta is that the default camera is directly attached to the Player entity. The default camera comes with ready to use controls for a first and third person camera setting.

In Unity you could do something similar either with implementing your own solution in code or using a starter kit.

Default Camera

In Crayta the default camera is basically the Character entity automatically spawned in the game world from the Player template, so it won’t be exposed separately in the Entities World Tree.

Crayta provides a default first or third person camera with a number of settings to customize for each type.

Camera Primitive

Additional cameras can be spawned in the game world to be used later during game play for cutscenes, level previews, as a background when showing game results etc.

Camera entities spawned in the world remain inactive until activated from a script.

How to select a camera?

When starting any Crayta game your spawned User entity in the game world automatically sets the selected camera to the default. And that would be your Character entity since in Crayta:

The default camera is the character entity.

At any point in a Crayta script you can change the selected camera to any other camera entity using the following code:

userEntity:SetCamera(cameraEntity)

You can also specify an optional second parameter as the transitionTime in seconds. That will automatically animate the view from the current camera to the newly selected one:

userEntity:SetCamera(cameraEntity, 3.0)

At any moment you can fallback to the default camera by calling the same method with no parameters:

userEntity:SetCamera()

And lastly you can query the active camera using a similar method:

userEntity:GetCamera()

If the default camera is selected, the Character entity will be returned. Otherwise a camera entity will be referenced.

It’s a simple and powerful mechanism that allows you to present dynamic views of your game world to the player.