- Home /
Didn't explain myself well enough, have asked a different but related question since then.
Camera Follow Mouse
Hi there
I've got this piece of code for my camera (thanks to @simco500) to look around the player, according to the mouse position. It works like a charm, except I can't adjust how far the camera can travel. Simpy put, I'd like to give it boundaries/tell it how far out it can go. Could anyone help me out?
#pragma strict
var Damping = 6.0;
var Player : Transform;
var Height : float = 13.0;
var Offset : float = 0.0;
private var Center : Vector3;
function Update ()
{
var mousePos = Input.mousePosition;
var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
var PlayerPosition = Player.position;
Center = Vector2((PlayerPosition.x + CursorPosition.x) / 2, (PlayerPosition.y + CursorPosition.y) / 2);
transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
}
Here's a shot of just how way too far it travels.
Cheers!
Answer by miahmiah · Apr 19, 2016 at 11:22 PM
I would change this part:
Center = Vector2((PlayerPosition.x + CursorPosition.x) / 2, (PlayerPosition.y + CursorPosition.y) / 2);
Add these definitions:
var changex : float = 0.0;
var changey : float = 0.0;
var limitx : int = 100;
var limity : int = 100;
And make it like this:
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);
Cheers, that almost fixes it!
Only problem is it's not affecting $$anonymous$$us values. Camera can still freely move to left and down, up and right is getting restricted though.
Thanks!
Answer by incorrect · Apr 19, 2016 at 10:22 PM
Just use Mathf.Clamp to limit your center's position.
float horizontalLimit = 1f;
float verticalLimit = 1f;
Center.x = Mathf.Clamp(Center.x, -horizontalLimit, horizontalLimit);
Center.y = Mathf.Clamp(Center.y, -verticalLimit, verticalLimit);
Like this?
#pragma strict
var Damping = 6.0;
var Player : Transform;
var Height : float = 13.0;
var Offset : float = 0.0;
var horizontalLimit = 1000f;
var verticalLimit = 1000f;
private var Center : Vector3;
function Update ()
{
var mousePos = Input.mousePosition;
var CursorPosition : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
var PlayerPosition = Player.position;
Center.x = $$anonymous$$athf.Clamp(Center.x, -horizontalLimit, horizontalLimit);
Center.y = $$anonymous$$athf.Clamp(Center.y, -verticalLimit, verticalLimit);
transform.position = Vector3.Lerp(transform.position, Center + Vector3(0,Height,Offset), Time.deltaTime * Damping);
}
Doesn't work for me, camera stopped moving
I gave you the idea how to limit camera movement. But you still have to understand what your code is doing. It stopped moving because you completely removed center point calculation, so now you are clamping zero vector.
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
How do I let a camera follow on one axis? 3 Answers
how to stop the camera to follow the player on his jump movement ? 2 Answers
Camera position not updating 1 Answer
Having a 3D text appear in the middle of the screen without using GUI (C#) 2 Answers