限制相机旋转- Unity Clamp Quaternion
Limit camera rotation- Unity Clamp Quaternion
我有这段代码可以让相机通过触摸屏的输入进行旋转,我需要将旋转锁定在特定范围内。我尝试使用“Math.Clamp”失败。有人可以帮助我吗?
我尝试输入此字符串,但视觉对象收到一条错误消息,显示“无法从 'UnityEngine.Quaternion' 转换为 'float'”
rotationY = Mathf.Clamp(rotationY, -30, 30);
我把允许相机 运行 无限制的代码留给你。
感谢大家的帮助
private Touch touch;
private Vector2 touchPosition;
private Quaternion rotationY;
private float rotateSpeedModifier = 0.01f;
//public float yPos;
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Moved)
{
rotationY = Quaternion.Euler(0f, -touch.deltaPosition.x * rotateSpeedModifier, 0f);
transform.rotation = rotationY * transform.rotation;
Debug.Log(rotationY * transform.rotation);
}
}
}
你不能夹住 Quaternion
。
然而,您可以存储您已经旋转的角度的 float 并将其夹紧:
private Touch touch;
private Vector2 touchPosition;
private float rotationY;
private float rotateSpeedModifier = 0.01f;
private Quaternion originalRotation;
private void Start ()
{
originalRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Moved)
{
rotationY -= touch.deltaPosition.x * rotateSpeedModifier;
rotationY = Mathf.Clamp(rotationY, -30, 30);
transform.rotation = originalRotation * Quaternion.Euler(0, rotationY, 0);
}
}
}
我有这段代码可以让相机通过触摸屏的输入进行旋转,我需要将旋转锁定在特定范围内。我尝试使用“Math.Clamp”失败。有人可以帮助我吗?
我尝试输入此字符串,但视觉对象收到一条错误消息,显示“无法从 'UnityEngine.Quaternion' 转换为 'float'”
rotationY = Mathf.Clamp(rotationY, -30, 30);
我把允许相机 运行 无限制的代码留给你。 感谢大家的帮助
private Touch touch;
private Vector2 touchPosition;
private Quaternion rotationY;
private float rotateSpeedModifier = 0.01f;
//public float yPos;
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Moved)
{
rotationY = Quaternion.Euler(0f, -touch.deltaPosition.x * rotateSpeedModifier, 0f);
transform.rotation = rotationY * transform.rotation;
Debug.Log(rotationY * transform.rotation);
}
}
}
你不能夹住 Quaternion
。
然而,您可以存储您已经旋转的角度的 float 并将其夹紧:
private Touch touch;
private Vector2 touchPosition;
private float rotationY;
private float rotateSpeedModifier = 0.01f;
private Quaternion originalRotation;
private void Start ()
{
originalRotation = transform.rotation;
}
// Update is called once per frame
void Update()
{
if(Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Moved)
{
rotationY -= touch.deltaPosition.x * rotateSpeedModifier;
rotationY = Mathf.Clamp(rotationY, -30, 30);
transform.rotation = originalRotation * Quaternion.Euler(0, rotationY, 0);
}
}
}