"Smooth Follow" Camera

0 favourites
  • 6 posts
From the Asset Store
Simple and easily editable template for a dynamic camera that zooms in and out based on how far apart the players are.
  • I'm working on a top-down racing game.

    OBJECTS:

    • Car
    • Camera
    • Camera_Target

    The Camera_Target (henceforth: Target) is pinned to the Car at a distance in front of the Car. The Camera is always trying to follow and reach the Target. So as the Car moves about, the Target is always in motion, too. The Camera thus seeks to "catch up".

    Presently, I'm using the action for the Camera: lerp(Self.X, Target.X, 0.075) [same for the Y value].

    I've also tested using lerp(Self.X, Target.X, 1-dt^0.075)

    Now, this works...but the problem I have is that there is the occasional "jerkiness" or "jolt" as the Camera approaches its target coordinates of the moving Target object (this is especially more noticeable when using 1-dt*0.075).

    The effect I'm going for is a smooth transitional movement of the Camera that ultimately eases into position just ahead of the Car in whatever direction, even when the Car turns suddenly or quickly. I do realize that there is the recent addition of the Tween behavior but that doesn't work well "every tick", and I haven't found any other better solution.

    Can anyone assist in addressing the "jolt" issue, please?

  • There shouldn't be any jerkiness with the first formula, although the correct method is to use dt: lerp(Self.X, Target.X, dt*5)

    The second formula should give you very sharp movement. At 60 fps dt value is ~0.016s, so (1-dt^0.075) gives you something around ~0.3, which means that the camera will cover 1/3 of the distance to target every tick. This is a lot, that's why this camera will follow the car almost instantly.

    You can also try something like this: lerp(Self.X, Car.X, abs(car.X-Self.X)/1000), this will give the camera slow start and slow finish. There are lots of other methods, but I don't remember them :)

    In C2 I also used MoveTo behavior for the camera instead of lerp. On every tick moving to target position with smooth acceleration and deceleration and limited maximum speed, it looked pretty good. MoveTo addon is ported to C3, so you can try it if you want.

    Here is a little demo to play with:

    dropbox.com/s/afrx680gfapj4yr/Lerping.capx

    .

    By the way, here is an excellent article explaining how cameras work in many popular game: Scroll Back: The Theory and Practice of Cameras in Side-Scrollers

  • Thank you so much! I'll try out your suggestion!

  • In your demo, can you break down what's happening in your fourth instance, please?

    If it was translated to English, how would it be read? LOL

  • Self.X>Car.X?180:0 this means if camera.X>car.x, then the angle is 180, otherwise the angle is 0.

    abs(car.X-Self.X)*4*dt means absolute distance between camera and car (always positive value) multiplied by 4*dt, which is ~0.06

    So the camera sprite moves towards car sprite, and the bigger is the distance between sprites, the faster it moves.

    .

    Actually, now I realize that you don't need abs() and that formula for angle. Simply move (car.X-Self.X)*4*dt pixels at angle 0 - it will work the same.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Awesome! I think that makes sense. I found instance 3 more suitable but I may rest the 4th after all.

    Thank you as always for your help!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)