2015-05-29 37 views
2

我用相機創建第三個人。相機將與鼠標移動同步移動。我爲相機移動創建了腳本,但是當速度很高時,一切都開始動搖了。光滑的相機旋轉團結

如何強制相機移動平穩?

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

看看:http://docs.unity3d.com/ScriptReference/Vector3.Lerp。 html和http://docs.unity3d.com/ScriptReference/Mathf.Lerp.html和https://unity3d.com/learn/tutorials/modules/beginner/scripting/linear-interpolation – JinJi

+0

另外,請考慮添加語言你的代碼在標籤中(如C#或Javascript),它會爲你的代碼添加着色它更具可讀性。 –

回答

0

我建議使用線性插值(lerp函數),因爲在評論中也提示JinJi。

Here是官方教程

的線性插值函數將在平滑的間隔時間的值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);