- Home /
rotate camera on mouse reaching edges
Hi, im working on a camera script where camera follows character and rotates when mouse is close to screen edges. I have 2 main issues, first, when coming from the "rotation" area to the "non rotation" area transform just snaps and i would like to keep rotation or the "angle" on character the second issue is that i would like the camera to just rotate left when the mouse is on the left side of the screen and not rotate back when coming back to center(same for right side) and increase rotation speed when getting closer to an edge.
var ScreenWidthPercentage: float;
var ScreenHeightPercentage: float;
var rotationzone: boolean;
var target : Transform;
var distance = 10.0;
var height:float=2.8;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -15;
var yMaxLimit = 80;
private var x = 0.0;
private var y = 0.0;
private var newRotation: Quaternion;
private var oldRotation: Quaternion;
function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}
function Update(){
// gives us 25% of the size of the screen (change the * 25 to choose the percentage you want)
ScreenWidthPercentage = (Screen.width * 25) / 100;
ScreenHeightPercentage = (Screen.height * 25) / 100;
// when the cursor gets to less than the ScreenWidthPercentage from the left or right edge of the screen
if(Input.mousePosition.x <= ScreenWidthPercentage || Input.mousePosition.x >= Screen.width - ScreenWidthPercentage || Input.mousePosition.y <= ScreenHeightPercentage || Input.mousePosition.y >= Screen.height - ScreenHeightPercentage){
//turn on the SmoothFollow script
rotationzone = true;
}
else {
// turn off the follow script, because we're no longer at the edge of the screen
rotationzone = false;
}
}
function LateUpdate () {
if (target) {
if(rotationzone){
if(Input.mousePosition.x <= ScreenWidthPercentage){//xSpeed related to mousepositionleft
xSpeed = (ScreenWidthPercentage-Input.mousePosition.x)*10;
}
if(Input.mousePosition.x >= Screen.width - ScreenWidthPercentage){//xSpeed related to mousepositionright
xSpeed = (Screen.width*0.75-Input.mousePosition.x)*-10;
}
if(Input.mousePosition.y <= ScreenHeightPercentage){//xSpeed related to mousepositiontop
ySpeed = (ScreenHeightPercentage-Input.mousePosition.y)*1;
}
if(Input.mousePosition.y >= Screen.height - ScreenHeightPercentage){//xSpeed related to mousepositionbottom
ySpeed = (Screen.height*0.75-Input.mousePosition.y)*-1;
}
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, height, -distance) + target.position;
transform.rotation = Quaternion.Lerp (oldRotation, rotation, Time.deltaTime * 0.1f);
transform.position = position;
}
if(rotationzone==false)
position = rotation * Vector3(0.0, height, -distance) + target.position;
transform.rotation = rotation;
transform.position = position;
}
oldRotation=transform.rotation;
}
static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
Answer by segment · Feb 03, 2014 at 11:12 AM
Hi Mayers. I was facing the same issue that you and here is my code for you to try.
protected void processMove(Vector3 delta)
{
//Moving crosshair
crosshairPosition = new Rect(crosshairPosition.x + delta.x,
crosshairPosition.y - delta.y, crosshairTexture.width, crosshairTexture.height);
//make weapon 'look' at crosshair.
var distance = (-weapon.transform.position.z * - mainCamera.transform.position.z) * 0.5f;
var position = new Vector3(crosshairPosition.x + crosshairTexture.width/2,
Screen.height - crosshairPosition.y - crosshairTexture.height/2, distance);
position = mainCamera.ScreenToWorldPoint(position);
weapon.transform.LookAt(position);
if (crosshairPosition.x <= 150f + crosshairTexture.width/2 &&
delta.x < previousDeltaX)
{
rotationX += delta.y * -1 * Time.deltaTime * rotationSpeed;
rotationY += delta.x * Time.deltaTime * rotationSpeed;
rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
rotationY = Mathf.Clamp(rotationY, minRotationY, maxRotationY);
previousDeltaX = delta.x;
transform.localEulerAngles = new Vector3(rotationX % 360, rotationY % 360, 0);
}
else if (crosshairPosition.x >= Screen.width - 200f - crosshairTexture.width/2
&& delta.x > previousDeltaX)
{
rotationX += delta.y * -1 * Time.deltaTime * rotationSpeed;
rotationY += delta.x * Time.deltaTime * rotationSpeed;
rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
rotationY = Mathf.Clamp(rotationY, minRotationY, maxRotationY);
previousDeltaX = delta.x;
transform.localEulerAngles = new Vector3(rotationX % 360, rotationY % 360, 0);
}
else if (crosshairPosition.y <= 100f - crosshairTexture.height/2
&& delta.y > previousDeltaY)
{
rotationX += delta.y * -1 * Time.deltaTime * rotationSpeed;
rotationY += delta.x * Time.deltaTime * rotationSpeed;
rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
rotationY = Mathf.Clamp(rotationY, minRotationY, maxRotationY);
previousDeltaY = delta.y;
transform.localEulerAngles = new Vector3(rotationX % 360, rotationY % 360, 0);
}
else if (crosshairPosition.y >= Screen.height - 100f - crosshairTexture.height/2
&& delta.y < previousDeltaY)
{
rotationX += delta.y * -1 * Time.deltaTime * rotationSpeed;
rotationY += delta.x * Time.deltaTime * rotationSpeed;
rotationX = Mathf.Clamp(rotationX, minRotationX, maxRotationX);
rotationY = Mathf.Clamp(rotationY, minRotationY, maxRotationY);
previousDeltaY = delta.y;
transform.localEulerAngles = new Vector3(rotationX % 360, rotationY % 360, 0);
}
else
{
previousDeltaY = 0f;
previousDeltaX = 0f;
}
}
delta is a diff between last cursor position in a last frame and current one in current frame.
Though I'm rotating my 'shooter' game object not the camera itself but you can pick up the idea (not so best but working though) of previous delta and delta.
Feel free to ask questions, Good luck!