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);
}