# How to have Camera following a Target, but not directly centered?

I am using the CameraScroll with the target as my player. I want the player to be able to move freely in the cameras view and only when he nears the edge of the camera's FOV would it then shift either up/down and left/right depending on what portion of the screen he moves in.

I saw in the 2D tortorial something about a springiness, that makes the camera lag which could be another solution. I played with the Drag variable, but this doesn't appear to be changeing anything.

One possible solution is to keep a separate game object that is focussed by the camera (instead of directly focussing the player). You could then check the distance between this object ("focuspoint") and the player and when it exceeds a certain amount, lerp this object towards the exact player position (which will "move the camera").

As I think you're in a 2D environment, instead of calculating the real distance between the focuspoint and the player, you could just calculate distance on x and distance on y and also have separate threshold values (those are subject to tweaking for the best player experience). This will give you somewhat more precise control and also will cost a little less performance.

Make a script where the camera continuously goes to a destination point, then on your characters update set the destination point to somewhere behind him.

Depending on your code, the camera moving to the destination point will have some quick n cheap physics or fancier ones...

I recommend an exponential velocity based on distance to the point or a logrithmic decay

You are correct in that this is a top down 2D game. Thanks for the help.

I went with Jashan's solution and although this is a very simple version and I may want to play with a little more, it does do the trick. Like he said I created an empty game object called Focal Point and attached the script below. Jeston I see how your solution works as well and if this doesn't work then I may apply yours instead.

//Player Object or whatever you deem necessary var Object1 : GameObject; //Modified variable for if Ojbect1 and our Focal Point get too far from each other var threshold : int = 0; //private variable for distance private var dist;

function Start(){ //First let's start with our Camera Centered on our Player transform.position = Object1.transform.position; }

function Update () { //Check the distance to see how far the Object1 and Focal Point are dist = Vector3.Distance(transform.position, Object1.transform.position);

``````     //If distance exceeds our Threshold then Lerp our Focal Point to new location
if(dist >= threshold){

transform.position = Vector3.Lerp (transform.position,Object1.transform.position,
Time.deltaTime);
}

``````

}

Now of course set you Object1 in the Inspector to be what you need free moving, in my case the player. Then you can play with the Threshold to see what works, I have it low 10 currently because as the player goes to the top of the screen I dont want him getting hit by enemy fire he cant see. I will have to play with this more I can already tell.

If anyone needs to know I just have the Camera Scrolling Script on the MainCamera gameobject with it set as the target.

On a side note, I think you could use the for a Fairy or Floating Gun Turrent to Follow your Player around the level, pretty cool.