Move Camera 2D Towards Mouse Position
Hi there, I'm working on a 2D game, top down. Here's a picture:
Currently the camera is just a child of the player and follows him around. I'm interested in adding a slight look-around feature, so the camera can explore in -x,-y,x,y directions depending on the mouse position. So if he moved the mouse top left:
Camera moves a limited amount in that direction. I've tried it out but can't make it happen. My current script looks like this:
#pragma strict
var Damping = 6.0;
var Player : Transform;
var Height : float = 13.0;
var Offset : float = 0.0;
var changex : float = 0.0;
var changey : float = 0.0;
var limitx : int = 100;
var limity : int = 100;
private var Center : Vector3;
function Update ()
{
var mousePos = Input.mousePosition;
var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
var PlayerPosition = Player.position;
changex = (PlayerPosition.x + CursorPosition.x) / 2;
changey = (PlayerPosition.y + CursorPosition.y) / 2;
if (changex > limitx) { changex = limitx; }
if (changey > limity) { changey = limity; }
Center = Vector2(changex, changey);
transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
}
And this is nowhere from working. The camera is limited from ever passing position x and position y in the world. It has no boundaries and travels as far as it can with the mouse.
Can you guys help me with the script please? I'm guessing the correct way of doing it would be telling the camera where center of screen is,then by a raycast tell how far out it can move and position itself from there to the mouse, 360? I don't know how to write it though :/
Cheers!