Follow Camera
The FollowCamera can be used to follow the movement of a specific character or entity in your game.
FollowCamParams
The follow camera params are the parameters that are used to help control the follow camera.
Properties:
Property | Type | Functionality |
---|---|---|
min_x | float | The minimum x position in the world that the camera can be. The camera will stop moving once it reaches this position. |
min_y | float | The minimum y position in the world that the camera can be. The camera will stop moving once it reaches this position. |
max_x | float | The maximum x position in the world that the camera can be. The camera will stop moving once it reaches this position. |
max_y | float | The maximum y position in the world that the camera can be. The camera will stop moving once it reaches this position. |
scale | float | Usually the current scale of the main camera. Used in the movement and boundary calculations internally. |
springback | float | This controls how fast the camera follows the player. Range [0 - 1] . If the springback is set to 1, the camera will follow directly with the player. Values less than 1 will lag behind the player and catch up when the player stops or the camera boundaries have been reached. |
Examples
-- There are two ctors for FollowCamParams
-- First takes a lua table {}
local followCamParams1 = FollowCamParams(
{
min_x = 0,
min_y = 0,
max_x = WindowWidth(), -- Get the Window Width from the engine
max_y = WindowHeight(), -- Get the Window Height from the engine
scale = 4.0, -- Set the camera scale to 4
springback = 0.2 -- Follow slightly behind the character
}
)
-- The Second ctor uses arguments
local followCamParams2 = FollowCamParams(
0, 0, WindowWidth(), WindowHeight(), 4.0, 0.2
)
FollowCamera Functionality
In order to create a new follow camera, we need to pass in the Desired FollowCamParams and the Entity that we want the camera to follow.
Function | Description |
---|---|
update() | Updates the position of the camera based on the FollowCamParams and the current position of the player. The camera will center around the player, unless the camera has reached it's boundaries. |
set_params(FollowCamParams) | Re-sets the follow cam parameters to the new desired parameters |
set_entity(Entity) | Sets a new entity for the camera to follow. This allows the user to change what the camera is following on the fly. |
set_springback(float) | This will adjust the springback to the desired value. The springback is clamped to [0 - 1] . |
get_params() | Returns the current FollowCamParams |
Example
-- Create a new followCam
local followCam = FollowCamera(
FollowCamParams(0, 0, WindowWidth(), WindowHeight(), 4.0, 0.5),
playerEntity -- We need to pass in the entity we want the camera to follow.
)
-- We need to always update the camera in our main update loop
-- This will ensure that the camera is following the player's
-- movement
followCam:update()