Unity RotateAround()与特定方向结合

时间:2019-01-08 13:50:22

标签: c# unity3d camera rotation

我正在尝试围绕人物旋转相机,并使其朝向特定方向。例如,我希望摄像机围绕角色旋转,并在面向另一个对象前向轴时停止旋转。 所以我想我需要RotateAround()和LookRotation()之间的组合。 RotateAround()可以使镜头围绕角色旋转,LookRotation()可以使镜头也面向该对象。

问题是:我不想让摄像机移动或旋转远离角色。

我尝试了几件事,最想到的是:

private void RotateCamera()
{
    Quaternion currentCameraRotation = _camera.transform.rotation;
    Quaternion futureCameraRotation = Quaternion.LookRotation(_hitObstacleStartingPoint.transform.forward);
    float rotationAngle;
    Vector3 rotationAxis;
    Quaternion.FromToRotation(currentCameraRotation.eulerAngles, futureCameraRotation.eulerAngles).ToAngleAxis(out rotationAngle, out rotationAxis);
    _camera.transform.RotateAround(_character.transform.position, _camera.transform.up, rotationAngle);
}

但这最终导致我的相机到处旋转。

谢谢!

编辑:对不起我的糟糕解释。 这是一张小图。 enter image description here

2 个答案:

答案 0 :(得分:0)

如果您计算当前旋转角度和目标旋转角度之间的距离,并且可以随时间绕枢轴旋转。

参见下文:

private void RotateCamera()
{
    Quaternion currentCameraRotation = _camera.transform.rotation;
    Quaternion futureCameraRotation = Quaternion.LookRotation(obstacleObject.transform.forward, obstacleObject.transform.up);

    float angleDelta = Quaternion.Angle(currentCameraRotation, futureCameraRotation);

    _camera.transform.RotateAround(this._character.transform.position, _camera.transform.up, angleDelta * rotationSpeed * Time.deltaTime);        
}

为了帮助可视化正在发生的事情:

void OnDrawGizmos()
{
    Gizmos.color = Color.green;
    Gizmos.DrawLine(_camera.transform.position, _camera.transform.forward * 20 + _camera.transform.position);

    Gizmos.color = Color.red;
    Gizmos.DrawLine(obstacleObject.transform.position, obstacleObject.transform.forward * 20 + obstacleObject.transform.position);
}

如果您的障碍物绕着x,z旋转,则此方法无效它总是假设一个身份转发向量。

答案 1 :(得分:0)

我最终在场景中使用了固定的相机点。它避免了这个问题,所以我不认为这个问题可以解决,但是我在这个项目上有最后期限,因此我不能再花太多时间在这个问题上了。感谢那些帮助我的人。