How to Become That Silky Smooth Camera Movement in Unity: A Guide for Writing a Basic C# Photographic camera Script for Your 2D Platformer

I think that i of the most effective tricks for increasing the quality of your 2d platformer is to have a good photographic camera system. At that place are (of course) several ways to achieve this but if you observe your linear camera movement a bit boring and you desire to add a little smoothess, this guide could be something for you.

Let'due south beginning off with a few variables

If you oasis't already, add a script to your camera then you can declare some variables. Start with these lines.

                    using System.Collections; using System.Collections.Generic; using UnityEngine;  public class CameraMovement : MonoBehaviour {   public Transform player;   public Vector3 cameraOffset;   public bladder cameraSpeed = 0.1f; }                  

actor is going to be the positional component of your role player GameObject.

cameraOffset is going to be the positional offset vector (the distance and angle) between your histrion and the camera.

cameraSpeed is going to be the movement speed of the camera.

Prepare the initial camera position

Since player is a Transform component, you tin elevate a GameObject into its field in the Inspector Window. If you do that, you can hands prepare the camera position = player position + offset vector within the Start () role.

                    using Arrangement.Collections; using System.Collections.Generic; using UnityEngine;  public class CameraMovement : MonoBehaviour {   public Transform player;   public Vector3 cameraOffset;   public float cameraSpeed = 0.1f;    void Start()   {     transform.position = player.position + cameraOffset;   } }                  

Adjust the offset vector in the Inspector Window to go whatever camera view you want for your game.

At present follow the Role player!

A linear camera system would follow the histrion with a abiding speed. That's not polish at all.
I prefer using linear interpolation to make the camera system non-linear.

MichaelScott.gif

Yes. And it makes sense. A linear interpolation will over time produce a non-linear motion if its interpolant is a function of an inconstant altitude.
But you lot don't need to know that stuff because Unity has an piece of cake vector part called Lerp (). Let's employ it within the FixedUpdate () office.

                    using System.Collections; using System.Collections.Generic; using UnityEngine;  public class CameraMovement : MonoBehaviour {   public Transform player;   public Vector3 cameraOffset;   public bladder cameraSpeed = 0.1f;    void Outset()   {     transform.position = player.position + cameraOffset;   }    void FixedUpdate ()   {     Vector3 finalPosition = histrion.position + cameraOffset;     Vector3 lerpPosition = Vector3.Lerp (transform.position, finalPosition, cameraSpeed);     transform.position = lerpPosition;   } }                  

finalPosition is the concluding photographic camera position.

lerpPosition is the current position of the camera.

cameraSpeed can accept a value between 0 and one but something like 0.ane will probably be suitable.

That is basically information technology. Beneath is a comparison betwixt a linear and an interpolated camera system. If you lot haven't tried it out yourself yet, you should! Information technology really makes a big divergence.

LinearMotion.gif

InterpolatedMotion.gif

At that place is always room for improvement

This photographic camera system could be improved even further by adding positional threshold values that the player needs to exceed before the camera movement is initiated. Basically, a system where the photographic camera starts moving when the player get close to the edge of the screen. I could too add together adjustments for when the role player's acceleration is greater than that of the photographic camera, e.g. when the player is falling.

I might write another post almost this anytime.