- Home /
How to make this camera movement smoother without removing the limitations
Hello,
I would like this camera movement code to be smoother, and I have no idea how to implement that. If you could post a solution then ill be very thankful..
Thanks
public class LimitedCamera : MonoBehaviour
{
public float LimitAngleX = 10f;
public float LimitAngleY = 10f;
private float AngleX;
private float AngleY;
public void Update()
{
var angles = transform.localEulerAngles;
var xAxis = Input.GetAxis("Mouse X");
var yAxis = Input.GetAxis("Mouse Y");
AngleX = Mathf.Clamp(AngleX - yAxis, -LimitAngleX, LimitAngleY);
AngleY = Mathf.Clamp(AngleY + xAxis, -LimitAngleY, LimitAngleY);
angles.x = AngleX;
angles.y = AngleY;
transform.localRotation = Quaternion.Euler(angles);
transform.localEulerAngles = angles;
}
}
Answer by BuzzyRoboYT · May 18 at 09:56 AM
What do you mean by smooth? Im not sure exactly how to help you, maybe if you can give me a bit more information about your problem.
Answer by tristantcate · May 18 at 10:39 AM
You might want to look into Vector3.SmoothDamp.
At the end of your script, where you simply set transform.localRotation, remove both
transform.localRotation = Quaternion.Euler(angles);
transform.localEulerAngles = angles;
And change it to something like:
transform.localEulerAngles = Vector3.SmoothDamp(transform.localEulerAngles, angles, ref currentVelocity, smoothTime);
Don't forget to add these to the top of your script:
float currentVelocity;
float smoothTime = 0.2f; //(Or however fast you want the smoothing to be.)
Or you can check out the Vector3.Lerp.
It is very similar to SmoothDamp.
Your answer
Follow this Question
Related Questions
Smooth dizzy camera shake. 0 Answers
Smooth Follow Problem 1 Answer
Smooth Camera Movement 1 Answer