# How do I make my camera follow the player?

So I have the camera follow the player in a very basic sense. It Lerps to the same X and Z coordinates of the player and always maintains 16 units above the player in the Y axis. What I want the camera to be doing is to be constantly following the midpoint between the player’s position and the current position of the mouse on screen. That way, the closer the mouse to the player the more centered the camera is, but the more the player moves the mouse away the further the player can see. What I have doesn’t work, and unless the mouse is perfectly centered on the player on the screen the camera accelerates way off of where I want it to be.

``````#pragma strict

var cameraSpeed : float;
var cameraHeight : float;

private var target : Transform;
private var mousePos : Vector3;
private var targetPos : Vector3;

function Awake (){
cameraSpeed = 3.5;
cameraHeight = 16;
target = GameObject.FindWithTag("Player").transform;
}

function FixedUpdate (){
targetPos = new Vector3(target.position.x, target.position.y+cameraHeight, target.position.z);
mousePos = Input.mousePosition;
mousePos.x = Screen.width - mousePos.x;
mousePos.z = Screen.height - mousePos.y;
var pos : Vector3 = new Vector3((targetPos.x + mousePos.x)/2, targetPos.y, (targetPos.z + mousePos.z)/2);
transform.position = Vector3.Lerp(transform.position, pos, Time.deltaTime*cameraSpeed);
}
``````

My game is a top down 2D / 3D game where the player is constantly facing the mouse, but for some reason doing a task that sounds as simple as moving the camera to a midpoint of two locations is not so easy…

Make an object that is always the midpoint between the mouse position and the player and then have the camera have the SmoothFollow script and make the target the object.