# Making the Camera Follow the Mouse (Not LookAt)

Hi there.

I need a script that allows the camera to follow the mouse whilst staying focused on a point in the distance. The script I have so far is close but not really close enough.

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

public class MouseLookC : MonoBehaviour {

public GameObject m_Target;

// Update is called once per frame
void Update()
{
Vector3 screenResolution = new Vector3(Screen.width, Screen.height, 0.0f);
Vector3 mousePosition = Input.mousePosition;

mousePosition.y = Screen.height - mousePosition.y;
mousePosition.x = Screen.width - mousePosition.x;
screenResolution *= 0.5f;

Vector3 difference = mousePosition - screenResolution;
transform.Translate(difference * Time.smoothDeltaTime * -0.05f);
transform.LookAt(m_Target.transform);

Vector3 newPosition = transform.position;
newPosition.z = 50f;
transform.position = newPosition;
}
}

``````

The above script calculates the distance that the mouse is from the central point and uses that value to move the camera. All this script does is move the camera faster, the further the mouse is from the centre point but I want the camera to stick to the mouse a bit like this swf I created. Here

For left/right, subtract the screen's centre position from the current mouse position. (This will give you a negative value if the mouse if left of centre and positive if right, etc). Then, divide the resulting value by half of the screen's width - you will get a value that varies between -1 and +1 as you move the mouse from extreme left to extreme right. You can then multiply this value by the maximum angle you want to turn. You can use Quaternion.Euler to perform the actual rotation of the object by this angle. You can use the same basic idea for up/down rotation. To simplify things, it is often better to have your camera inside a parent object and rotate the parent for left/right motion, but rotate the camera object for up/down.

Here is a neutered version of what I use:

``````void Start ()
{
cameraVelocity = Vector3.zero;
zoomVelocity = 0.0f;
zoomTarget = height;

targetExtents = target.renderer.bounds.extents.magnitude * 0.75f;    //The game object I designed this to work with needed the * 0.75 your results may vary
}

void update()
{

float centerX = Screen.width * 0.5f;
float centerY = Screen.height * 0.5f;

float offsetX = Input.mousePosition.x - centerX;    //x = 0 is left of screen
float offsetY = Input.mousePosition.y - centerY;   //y = 0 is bottom of screen

offsetX = offsetX / centerX;   //these become -1 < offset < +1
offsetY = offsetY / centerY;

offsetX = Mathf.Clamp(offsetX, -1, 1);
offsetY = Mathf.Clamp(offsetY, -1, 1);

//height * tan(field of view) puts target at the edge of the screen, minus the tan of how big the target is at this distance keeps it's size in view

float maxOffsetMagnitude = height * (Mathf.Tan( ( camera.fieldOfView * Mathf.Deg2Rad) * 0.5f ) - targetExtents/height);

float aspectRatio = (float)Screen.width / (float)Screen.height;           //Warning, dividing ints, the cast is required
targetOffset = new Vector3(offsetX * aspectRatio, 0, offsetY) * maxOffsetMagnitude;

Offset = Vector3.SmoothDamp(Offset, targetOffset, ref cameraVelocity, smoothTime);

transform.position = Offset + target.position + target.up * height; //set camera position

}
``````

It works with the camera facing straight down which imo is more comfortable.