Hi, I’m facing a problem that I can’t seem to solve. I want the camera to follow the mouse cursor, but the player character to remain visible. So the camera can’t go a certain distance away from the player. Can anybody clear some stuff up for me?
I think I know what you mean, I’ve had the same problem but with a little thinking I got it.
I made a script for the camera where it calculates the middle between the Player and the position of the cursor. Then I only needed to make something that made the camera follow that position in the x and z direction and voila :).
You can tweak it as much if you want. This script I made was perfect for my project, I hope it’ll work with you too.
var Damping = 5.0;
var Player : Transform;
var Height : float = 4;
var Offset : float = 5;
private var Center : Vector3;
var ViewDistance : float = 5.0;
function Update ()
{
var mousePos = Input.mousePosition;
mousePos.z = ViewDistance;
var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos); /
var PlayerPosition = Player.position;
Center = Vector3((PlayerPosition.x + CursorPosition.x) / 2, PlayerPosition.y, (PlayerPosition.z + CursorPosition.z) / 2);
transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
}
Hey, I’ve tought a little about your problem and here’s what I did:
calculate the middle between the player and the position of the mouse in the world.
Let the camera smooth follow that middle.
Actually quite simple but it took me quite a while to come up with.
var Damping = 12.0;
var Player : Transform;
var Height : float = 13.0;
var Offset : float = 0.0;
private var Center : Vector3;
var ViewDistance : float = 3.0;
function Update ()
{
var mousePos = Input.mousePosition;
mousePos.z = ViewDistance;
var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
var PlayerPosition = Player.position;
Center = Vector3((PlayerPosition.x + CursorPosition.x) / 2, PlayerPosition.y, (PlayerPosition.z + CursorPosition.z) / 2);
transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
}