# 2D camera following in front of the player?

Hello, how do I make a camera follow in front of the player in 2D? My game is a top-down shooter. What I’m trying to achieve is that whenever the player moves, they get to see what is in front of them first and when they stop, the camera slowly centers its way back towards the player.

This is not the best example but it’s the only thing I can find:

Skip to 1:06 to see the effect I’m trying to achieve. You can see how when the crosshair moves to the right, the camera moves in a way that let the player see what is in front of them and the player is now in the left side of the screen. The only difference is that I want my camera to move when the player moves. So if they move up, the camera moves up too until the player is in the bottom of the screen. Thanks!

I’m currently using this code for my camera:

``````using UnityEngine;
using System.Collections;

{
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
public Transform target;

void FixedUpdate()
{
if (target)
{
Vector3 point = Camera.main.WorldToViewportPoint(target.position);
Vector3 delta = target.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
}
}
``````

It works perfectly fine except for the fact that when my player moves, the camera moves after a tiny delay so now the camera shows what is behind the player, not in the front. How do I transform the script into something that let the camera shows what is in front of the player?

1 Like

OK,

Well, Vector3.SmoothDamp is getting an average between the current camera position, and the player. Causing it to “slowly move” towards the player.

If you want it to move “ahead” of the player, then you need to rethink the formula (think: include an offset). And because you only want it to “sometimes” be ahead of the player, then you really need to rethink the formula (think: offset multiplied by velocity, and clamped).

The one above only works one way, all the time and really looks like it is just smoothing.

I know I need an offset and such but I’m stuck on how to get the idea in my head to work as a script. Can you give a basic foundation script that I can work on? I’ve been trying to get this to work for a couple of weeks now so I can move on to other parts of my project so yeah, it’s been kinda bugging me

Learning the code is the fun part

But if you keep track of the players last position. And compare it to its current position, you’ll get a Vector2 that indicates velocity.

Use that as a multiplier against the max distance, i.e. Vector3(10f,10f,10f) and it will push out the camera. The smoothing would then slowly bring it back in.

Yeah learning code is fun

So anyways, I tried my best to understand what you mean and what I came up with, of course, didn’t worked. Here is the code:

``````using UnityEngine;
using System.Collections;

{
public float dampTime;
//private Vector3 velocity = Vector3.zero;
public Vector3 maxDistance;
public Transform target;

private Vector3 velocity;
private Vector3 targetCurrentPos;
private Vector3 targetLastPos;

void Start()
{
targetLastPos = target.position;
}

void FixedUpdate()
{
if (target)
{
targetCurrentPos = target.position;
velocity = targetCurrentPos - targetLastPos;
transform.position = Vector3.SmoothDamp(transform.position, maxDistance, ref velocity, dampTime);
targetLastPos = targetCurrentPos;
}
/*
if (target)
{
Vector3 point = Camera.main.WorldToViewportPoint(target.position);
Vector3 delta = target.position - Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, point.z));
Vector3 destination = transform.position + delta;
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
*/
}
}
``````

This only brings my camera’s position to 2, 2, -10 on the screen (I set the data in the inspector). I think I misunderstand what you said by a huge margin so could you explain, if u have time, on what I did wrong and where should I fix my code? I’m a noob developer who’s starting out and have a basic understanding of C# itself, but I’m new to the whole UnityEngine API so there’s alot of stuff that I have to learn.