2
votes

I create third person with camera. The camera will move in sync with the mouse move. I create script for camera move, its work but when the speed is high then everything starts to shake.

How force the camera move smooth?

Vector3 rotation = new Vector3();
rotation = this.transform.rotation.eulerAngles;
float ver = Input.mousePosition.y - mouse_position.y;
difference_y = difference_y ?? ver;
rotation.y += (float)(((Input.mousePosition.x - mouse_position.x) / one_degree_per_pixel_hor));
rotation.x += (float)((-(Input.mousePosition.y - mouse_position.y) / one_degree_per_pixel_ver));
rotation.z = 0;

if (difference_y != 0) {
    //  Forward tilt
    if (difference_y < 0) {
        if (rotation.x > max_slope_forward && rotation.x < max_slope_back) rotation.x = max_slope_forward;
    }
    //  Back tilt
    else {
        if (rotation.x < max_slope_back && rotation.x > max_slope_forward && rotation.x != max_slope_forward) rotation.x = max_slope_back;
    }
}

difference_y = ver;
this.transform.Rotate(new Vector3(-Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X"), 0),Time.deltaTime*SpeedRotation);
rotation = this.transform.rotation.eulerAngles;
rotation.z = 0;
this.transform.rotation = Quaternion.Euler(rotation);
mouse_position = Input.mousePosition;
1
Also, please consider to add the language of your code in the tags (like C# or Javascript), it add coloration to your code and make it more readable. - Gary Olsson

1 Answers

0
votes

I suggest to use Linear Interpolation (lerp function) as also suggest JinJi in the comment.

Here is the official tutorial

The Lerp function will change the value from to the value to smoothly at the interval time.

Lerp(float from, float to, float time)

With your code, you code do something like this

// Save the original rotation
OrgRotation = this.transform.rotation.eulerAngles;
rotation = OrgRotation;
// Do your stuff here
this.transform.rotation = Quaternion.Lerp(OrgRotation, Quaternion.Euler(rotation), 0.5f);