【发布时间】:2017-06-30 00:58:00
【问题描述】:
我有一个相机,我想围绕一个点 (0,0,0) 向所有方向旋转,但我想在它上面放一个夹子,这样它就不会在该点上方或下方太远。我以前见过这个问题回答了左右方向,但从来没有回答过垂直方向。
我尝试将这两个问题的代码(基本上说的是同一件事)转换为在垂直方向上工作,但它在旋转的某些点上出现错误,我不知道为什么。
First Question, Second Question
这就是我尝试转换它的方式:
//how much we want to rotate by this frame
float rotX = Input.GetAxis("Mouse X") * rotSpeed;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed; //(before clamping)
//find current direction
Vector3 currentDirection = transform.position - Vector3.zero;
//find current angle between basis for clamp & where we are now
float angle = Vector3.Angle(Vector3.forward, currentDirection);
//finds out if it's up or down
if (Vector3.Cross(Vector3.forward, currentDirection).x < 0) angle = -angle;
//find out how much you can move without violating limits
float newAngle = Mathf.Clamp(angle + rotY, yMinLimit, yMaxLimit);
//grabs how much you are allowed to move the angle from the current angle
rotY = newAngle - angle;
//spinning the garden
transform.RotateAround(Vector3.zero, Vector3.up, rotX);
transform.RotateAround(Vector3.zero, transform.TransformDirection(Vector3.right), -rotY); //vertical rotation
如果有人知道使这项工作适用于 Y 轴的正确方法,或者钳制垂直旋转的不同方法,我会非常高兴听到它!太棒了!
【问题讨论】:
标签: c# unity3d camera rotation unity5