I have this camera script that orbits when I press the right mouse button but its all jerky how can I smooth its movement? also it seems it remembers where it was last time I pressed the right mouse but obviously first time player press right mouse button the camera is pointing to the ground, I dont quite like that >_<
public class CameraScript : MonoBehaviour {
//camera variables
public Transform cameraLookAtTaret = null;
private int mouseDown;
private GameObject MiTarget = null;
private float height = 0.0f;
private float rotationDamping = 3.0f;
private float heightDamping = 2.0f;
private float distance = 2.0f;
private float yMinLimit = -20;
private float yMaxLimit = 80;
private float xMinLimit = -100;
private float xMaxLimit = 100;
private float xSpped = 250;
private float ySpeed = 120;
private float x = 0.0f;
private float y = 0.0f;
private float distanceMin = 1f;
private float distanceMax = 4f;
/// <summary>
/// Here is where all the mouse input takes place, if you hold now mouse button down it will just follow your character and zoom in and out
/// if you hold down the right mouse button you can rotate around your player and zoom in and out
/// if you hold both left and right mouse button it will also turn your player
/// </summary>
void Start(){
MiTarget= GameObject.FindGameObjectWithTag("Target");
if (MiTarget != null){
cameraLookAtTaret = MiTarget.transform;
}
var angles = gameObject.transform.eulerAngles;
x = angles.x;
y = angles.y;
}
float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
void LateUpdate(){
if (cameraLookAtTaret != null){
var wantedRotationAngle = cameraLookAtTaret.eulerAngles.y;
var wantedHeight = cameraLookAtTaret.position.y + height;
var currentRotationAngle = gameObject.transform.eulerAngles.y;
var currentHeight = gameObject.transform.position.y;
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
gameObject.transform.position = cameraLookAtTaret.position;
gameObject.transform.position -= currentRotation * Vector3.forward * distance;
gameObject.transform.position = new Vector3(gameObject.transform.position.x, currentHeight, gameObject.transform.position.z);
gameObject.transform.LookAt(cameraLookAtTaret);
}
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpped * 0.02f;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
y = ClampAngle(y, yMinLimit, yMaxLimit);
x = ClampAngle(x, xMinLimit, xMaxLimit);
var rotation = Quaternion.Euler(y, x, 0);
var currentRotationAngle = gameObject.transform.eulerAngles.y;
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
gameObject.transform.position = cameraLookAtTaret.position;
gameObject.transform.position -= currentRotation * Vector3.forward * distance;
var currentHeight = gameObject.transform.position.y;
gameObject.transform.position = new Vector3(gameObject.transform.position.x, currentHeight, gameObject.transform.position.z);
gameObject.transform.rotation = rotation;
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*5, distanceMin, distanceMax);
}
}
}
I didn’t make the original and there are things I still dont understand, but I have modify quite a bit of it.