平时经常能遇到,摄像机绕某一点进行旋转的需求,比如调整视角、更加详细展示某个物体,换座位等。我最近也是帮同事优化了一个换座位时调整摄像机视角的需求,关键代码如下。
代码语言:csharp复制private void Update()
{
if (Input.GetKeyUp(KeyCode.LeftArrow))
{
StartCoroutine(RotateCamera(pivotPos, pivotPos.forward, 36*5));
}
if (Input.GetKeyUp(KeyCode.RightArrow))
{
StartCoroutine(RotateCamera(pivotPos, pivotPos.forward, -36*5));
}
}
IEnumerator RotateCamera(Transform target, Vector3 axis, int angle)
{
int direction = angle > 0 ? 2 : -2;
int times= Mathf.Abs(angle);
for (int i = 0; i < times; i =2)
{
mainCamera.transform.RotateAround(target.position, axis, direction);
yield return new WaitForEndOfFrame();
}
}