【发布时间】:2016-11-02 12:53:43
【问题描述】:
我正在尝试使用空格键更改主摄像机的位置和旋转。 改变位置我没有问题,但发现在 z 轴上旋转相机的问题。 最初旋转设置为 359.9f,按下空格键后旋转设置为 179.9f,但当我返回原始旋转 (359.9f) 时,旋转方向不同。
这是我的代码:
using UnityEngine;
using System.Collections;
public class Untitled : MonoBehaviour {
//Lerp Position
private Vector3 start;
private Vector3 end;
//Lerp Time
private float lerpTime = 3f;
private float currentLerpTime = 0;
// Camera Up - Down
public bool up = false;
void Update () {
start = Camera.main.transform.position;
end = new Vector3 (Camera.main.transform.position.x, 105, Camera.main.transform.position.z);
//Quaternions
Quaternion RotationA = Quaternion.Euler (0, 0, 179.9f);
Quaternion RotationB = Quaternion.Euler (0,0, 359.9f);
// Inputs
if (Input.GetKey (KeyCode.Space) && Camera.main.transform.position.y < 8f)
{
up = true;
currentLerpTime = 0;
lerpTime = 3f;
}
if (Input.GetKey (KeyCode.Space) && Camera.main.transform.position.y > 103f)
{
up = false;
currentLerpTime = 0;
lerpTime = 3f;
}
// When the camera is down (Lerp/Slerp)
if (up == false)
{
currentLerpTime += Time.deltaTime;
if (currentLerpTime >= lerpTime)
{
currentLerpTime = lerpTime;
}
float Perc = currentLerpTime / lerpTime;
start = Camera.main.transform.position;
Camera.main.transform.position = Vector3.Lerp (start, new Vector3 (Camera.main.transform.position.x, 105, Camera.main.transform.position.z), Perc);
Camera.main.transform.rotation = Quaternion.Slerp (Camera.main.transform.rotation, RotationA, Perc);
}
// When the camera is up (Lerp/Slerp)
if (up == true)
{
currentLerpTime += Time.deltaTime;
if (currentLerpTime >= lerpTime)
{
currentLerpTime = lerpTime;
}
float Perc = currentLerpTime / lerpTime;
end = Camera.main.transform.position;
Camera.main.transform.position = Vector3.Lerp (end, new Vector3(Camera.main.transform.position.x, 6, Camera.main.transform.position.z), Perc);
Camera.main.transform.rotation = Quaternion.Slerp (Camera.main.transform.rotation, RotationB, Perc);
}
}
}
我也尝试了另一种旋转方法,但没有任何改变:
Camera.main.transform.rotation = Quaternion.Slerp (Camera.main.transform.rotation, Quaternion.AngleAxis(179.9f, Vector3.forward), Perc)
Camera.main.transform.rotation = Quaternion.Slerp (Camera.main.transform.rotation, Quaternion.AngleAxis(359.9f, Vector3.forward), Perc)
我该如何解决?提前致谢。
【问题讨论】:
标签: unity3d camera rotation unityscript