I know there is a lerp function there that smoothens the camera but I wanted it to work the other way around. Lets say starting position of the player is at the left side of the camera/screen and when the players goes around the right edge (or as long as more than half width) screen travelled I want to camera to snap to the player but with smooth transition and this is the hard part I wanted to make the camera move fast as long as it is less than half of the players distance travelled and apply a slow speed as the camera travels more than half the distance of player.
1 Answer
Let cameraX
and playerX
being the camera's and player's X position respectively. I assume that the camera is following the player only on the X axis (from left to right) and when the camera's X is the same as player's X, then the player is exactly in the middle of the camera view. I also assume that
If, for example, cameraX
is bigger than playerX
then cameraX
needs to decrease in value until it has the same value as playerX
. You can do something simple like:
cameraX = playerX;
This will make the camera "Jump" to follow the player, no matter how far away from the camera they get. If the player teleports outside of screen boundary, on the next frame the camera will follow.
Instead what you want, is to make the cameraX
constantly go closer and closer to the player. From the above example, you can do something like:
int camera_movement = 1;
int camera_collision = 10;
// Camera is on the right of the player - move it to the left
if (cameraX > playerX) {
cameraX -= camera_movement;
}
else
// Camera is on the left of the player - move it to the right
if (cameraX < playerX) {
cameraX += camera_movement;
}
/* Camera is close enough to the player, so just move it to the player
* this prevents the camera from keep jumping back and forth
*/
if ( Math.abs(playerX -cameraX) < camera_collision ) {
cameraX = playerX;
}
This moves the camera with a constant value towards the player, when its too close, it just "locks" on the player. Make sure to tweak camera_movement
and camera_collision
to suit your game.
This can work for some cases, but not for others. So another solution is:
cameraX = Math.Lerp(cameraX, playerX, 0.5);
This is using an integer Lerp
, that makes the camera keep moving towards the player based on the distance between them. For the last argument, if you give the value 0.5
, the camera will always travel half the distance between it and the player. If you give it a lower value, it will move there slower, but smoother.
Make sure you change some of the values based on your game. From what I understand, you mentioned the player can be on the left side of the camera only, in which case you can tweak the playerX
variable to something different than the player's X position, depending on where you want the camera to look at.
-
\$\begingroup\$ Does Lerp function has like a constant speed and can I make movements like fast to slow. \$\endgroup\$– ShulzCommented Aug 17, 2018 at 10:46
-
\$\begingroup\$ @Shulz If you put a constant value on
Lerp
, it will move smoothly from fast to slow. As explained in my answer, if you put0.5
the camera will move half the distance every update, which is a big value if the camera is far away, but a small value if the camera is close. Alternatively, nothing is stopping you from giving a variable toLerp
and controlling its value through some logic that you like. \$\endgroup\$ Commented Aug 17, 2018 at 11:26