Smooth movement of camera from one point to another

I have the camera following my character. During regular points in the game the character is deleted and recreated at a new point on the board (200px in x direction forward or back). Is there a way to make the camera smoothly go to that new position rather than jump instantly?

Maybe, something like this:

if camera.x > player.x
camera.x = camera.x-1

if camera.x < player.x
camera.x = camera.x+1


Here’s an example of x position movement.

It depends on what kind of camera you are using. For example, if it can move in both x and y direction, it would be a bit more complex.

Ahhhh, that would have been simpler! In the end I created a dummy object to act as a camera and move and stop when in certain distances from the player. Haha your way would have been much simpler :smiley: :smiley:

You can also do smooth camera stuff using the lerp function, roughly like this:

lerp(CameraX("",0),cam_destination.X(),0.03) lerp(CameraY("",0),cam_destination.Y(),0.03)

The last value (0.03) kind of defines how much the movement is smoothed, higher values have the camera move more promptly.

Hey! Could you explain how to exactly do this? Where and how use this function? Please :unamused:

This topic is very dead, next time open a new one please :wink:

The lerp interpolates two values linearly, you have a starting value A, an end value B and the interpolation value X determining in which point between A and B we are. The formula is:

A + X*(B - A)
  • If X = 0 > A + 0*(B - A) = A
  • If X = 1 > A + 1*(B - A) = B
  • If X = 0.5 > A + 0.5*(B - A) = 0.5A + 0.5B
  • If X = 0.9 > A + 0.9*(B - A) = 0.1A + 0.9B

See?, it “mixes” the two values, the interpolation value X must be between 0 and 1 to return values between A and B. You can use negative values or values greater than 1, but that wouldn’t be interpolation :stuck_out_tongue:

So for the camera movement, the idea is to save the starting camera position (A), we need the final camera position (B) and for the interpolation value we can use a timer:

Do = lerp(Variable(startingCameraX), Character.X(), TimerElapsedTime("cameraMovement")) to the X position of camera

Here the starting camera X position is saved in a variable, the final position is a Character object, and a timer “cameraMovement” moves the interpolation along time… it will reach the destiny in 1 second, you may want to restrict the timer value to 1 (for example with the “min” function).

1 Like

thank you for your answer Lizard-13 :orange_heart:
But still… I’m sooo green about coding, and I just have no idea which “condition” and “action” use to do this :frowning:

For now I have centered camera on my hero and added some depth to background.
I’m sorry I bother you with this… :pensive:


it is too easy !!

use this code … if you didn’t understand
watch this video

Please don’t bump threads over a year old, especially ones that started in 2016. :smile: