unity RotateAround() 结合特定方向
Unity RotateAround() in combination with a specific direction
我正在尝试围绕一个角色旋转我的相机,并使其朝向特定的方向。例如,我希望我的相机围绕我的角色旋转,并在它面向另一个对象前轴时停止旋转。
所以我想我需要 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);
}
但这最终导致我的相机到处旋转..
提前致谢!
编辑:对于我糟糕的解释,我深表歉意。
这是一个小图。
如果计算当前旋转角度与目标旋转角度之间的距离,则可以随着时间的推移绕轴心旋转。
见下文:
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 旋转,这将不起作用;它总是假设一个身份前向向量。
我最终在我的场景中使用了固定的相机点。它避免了这个问题,所以我不认为这个问题已经解决了,但是我在这个项目上有一个截止日期,我不能再花太多时间在这个问题上了。感谢那些帮助过我的人。
我正在尝试围绕一个角色旋转我的相机,并使其朝向特定的方向。例如,我希望我的相机围绕我的角色旋转,并在它面向另一个对象前轴时停止旋转。 所以我想我需要 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);
}
但这最终导致我的相机到处旋转..
提前致谢!
编辑:对于我糟糕的解释,我深表歉意。
这是一个小图。
如果计算当前旋转角度与目标旋转角度之间的距离,则可以随着时间的推移绕轴心旋转。
见下文:
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 旋转,这将不起作用;它总是假设一个身份前向向量。
我最终在我的场景中使用了固定的相机点。它避免了这个问题,所以我不认为这个问题已经解决了,但是我在这个项目上有一个截止日期,我不能再花太多时间在这个问题上了。感谢那些帮助过我的人。