- Home /
Question by
twoface262 · Jun 18, 2015 at 09:56 PM ·
unity 5cameramovementtopdown
Camera move with mouse
Hello!
I'm trying to get my camera to kind of lean towards where the mouse cursor is, but still keep my player as the focal point. My game is a top down game, and I'm looking for an effect similar to Angry Bots.
Right now my code kind of works, but when the player sprints he starts to go off screen and is no longer the focal of the camera. Thank you for the help!
Here is my code (C#):
public Transform target;
public float smoothTime = 0.3f;
public int CAMERA_BUFFER = 10;
public int PLAYER_CAM_DIFF_Y = 4;
private Vector3 velocity = Vector3.zero;
private void FixedUpdate() {
float centerX = Screen.width / 2;
float centerY = Screen.height / 2;
Vector3 mousePos = new Vector3((Input.mousePosition.x - centerX) / CAMERA_BUFFER, transform.position.y, (Input.mousePosition.y - centerY) / CAMERA_BUFFER);
Vector3 goalPos = new Vector3 (target.position.x, transform.position.y, target.position.z - PLAYER_CAM_DIFF_Y);
transform.position = Vector3.SmoothDamp (goalPos, mousePos, ref velocity, smoothTime);
}
Comment