In my 2D platformer game, I find it very exhausting for the player if the camera follows the player all the time. So, I looked up for some tricks to make camera movement feel more natural.

The first trick is from James Doyle, where the camera follows a target instead of the player itself. Thus, I can safely use Mathf.Lerp for the target, and avoid having jitters.

The second thing I want to do is to limit the vertical movement of the camera. That is, I don't want the camera to move up and down whenever the player jumps. It is dizzying. Instead, I want camera to follow the player only if player is vertically too high or too low.

Here is roughly what I do:


    public Transform cameraTarget;
    private void FixedUpdate()
       if(Mathf.Abs(transform.position.y - cameraTarget.position.y) > verticalLimit )
         cameraTarget.position = new Vector3(cameraTarget.position.x, Mathf.Lerp(cameraTarget.position.y, transform.position.y, Time.fixedDeltaTime), cameraTarget.position.z);
        if (Input.GetAxisRaw("Horizontal") != 0)
            cameraTarget.localPosition = new Vector3(Mathf.Lerp(cameraTarget.localPosition.x, Input.GetAxisRaw("Horizontal")* cameraAheadAmount, 0.8f * Time.fixedDeltaTime), cameraTarget.localPosition.y, cameraTarget.localPosition.z);


The problem for this approach is that whenever the vertical distance between the player and the camera target is less than the limit, the camera does not follow the player anymore. So, what I get is basically a camera following the player from down below, again, all the times.

I thought of using a boolean flag to tell the camera follow the player when vertical limit is exceeded, but then when do I set and unset the flag?

I am new to Unity's Update() and FixedUpdate() logic, so I cannot somehow design a workaround. Is there a simple way to do this?


Thanks to the comment of DMGregory (once more!), I could find what I wanted in this video.

Instead of making the camera follow the cameraTarget using

transform.position = new Vector3(target.position.x, target.position.y, transform.position.z);

I used the trick that was shown in the video. This is not exactly what I wanted, but even a better considering my problem.

transform.position = new Vector3(
(transform.position.x * .8f) + (target.position.x * .2f),
(transform.position.y*.95f) + (target.position.y*.05f),
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.