【问题标题】:Smooth camera rotation unity平滑相机旋转统一
【发布时间】:2015-08-12 06:19:09
【问题描述】:

我用相机创造第三人称。相机将与鼠标移动同步移动。我为相机移动创建脚本,它的工作但是当速度很高时,一切都开始摇晃。

如何使相机平滑移动?

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;

【问题讨论】:

标签: unity3d camera rotation unityscript


【解决方案1】:

我建议使用线性插值(lerp 函数),就像评论中建议的 JinJi 一样。

Here是官方教程

Lerp 函数会在间隔时间平滑地将值from 更改为值to

Lerp(float from, float to, float time)

使用您的代码,您可以编写类似的代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-12
    • 2015-12-19
    相关资源
    最近更新 更多