# Terraria-esque 'scope' function? (2D)

Hey,

I’ve been developing this game of mine for a couple of months now and I’ve been managing surprisingly well, many thanks to Unity Answers, however, I’ve come across something I’ve been unable to solve. Much like the function of the sniper scope/rifle in Terraria I’d like to have the camera focus on a point about half-way between the cursor and the player. So far I’ve just managed to somehow make the player fly very fast in the Z axis.

For those who don’t know what the scope is like:

When your cursor is on the player, the camera is centered, however, as you move your cursor outward the camera smoothly moves to roughly half way between the cursor and the player though the player is always in sight.

Explanation end.

I would provide my code, however, I fear it is horrendously wrong, considering the player catapulting (I have no idea why it’s happening.) It is probably best to start afresh with it, for me. Any insight on how to achieve such a thing would be excellent. Also, could it be in C# if possible. Though I’d be able to convert from JS, it’d be a little easier.

Thank you.

Well the focus point is just the mean of the player and mouse positions:

``````public Transform player;
public Transform camera;
pubic float cameraLag = 0.5f;
private float cameraZPosition;

void Start() {
cameraZPosition = camera.position.z;
}

void Update() {
//find ideal point for cameraFocus
Vector3 mousePosition = camera.ScreenToWorldPoint(Input.mousePosition);
Vector3 cameraFocus = (player.position + mousePosition) / 2;
cameraFocus.z = cameraZPosition;

//move camera closer to cameraFocus
Vector3 offset = cameraFocus - camera.position;
float ratioToMove = Mathf.Clamp(Time.deltaTime / cameraLag, 0.0f, 1.0f);
camera.position += offset * ratioToMove;
}
``````