Unity相机旋转时保持透视 [英] Unity Camera Maintain perspective while rotating

查看:352
本文介绍了Unity相机旋转时保持透视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我试图编写一个摄像头脚本,它将保持我在旋转时所看到的透视点。 > public void RegisterRotationControls()
{
var horizo​​ntal = CrossPlatformInputManager.GetAxis(InputAxisName.Horizo​​ntal.ToString());

offsetX = Quaternion.AngleAxis(horizo​​ntal * turnSpeed,Vector3.up)* offsetX;
transform.position = player.position + offsetX;

transform.LookAt(player.position + PlayerOffset);
}

目前这个脚本只有在摄像头看着玩家时才能正常工作,然而,相机有自由移动位置,但旋转应保持与中心的相对性。



第一行显示当前行为,而第二行显示应该发生的事情

$ b

解决方案

手动计算旋转以在保留原始(相对)面向的同时将相机移动到一个圆圈中有点繁琐,并且需要您做更多的数学



一个流行的解决方案是将相机放在一个空的GameObject下面,然后将GameObject放在要让相机旋转的点/对象上。 (这基本上是内部化了很多计算,让引擎完成繁重的工作。)然后,可以根据需要在本地翻译相机,然后旋转容器GameObject将相机移动一圈。


I'm trying to write a camera script which will maintain the perspective point which I'm looking at while rotating.

public void RegisterRotationControls()
{
    var horizontal = CrossPlatformInputManager.GetAxis(InputAxisName.Horizontal.ToString());

    offsetX = Quaternion.AngleAxis(horizontal * turnSpeed, Vector3.up) * offsetX;
    transform.position = player.position + offsetX;

    transform.LookAt(player.position + PlayerOffset);
} 

Currently this script is only working properly when the camera is looking at the player, however the camera has freedom to move position yet the rotation should maintain relativity to the center.

The top row dipicts the current behavior, while the bottom depicts what should be occuring

解决方案

Manually calculating the rotation to move a camera in a circle while retaining its original (relative) facing is somewhat cumbersome, and requires you to do a lot more math than you need to.

A popular solution is to parent the camera under an empty GameObject, and centering that GameObject on the point/object you want the camera to rotate around. (This basically internalizes a lot of the calculations, letting the engine do the heavy lifting.) Then, you can locally translate the camera as needed and then rotate the container GameObject to move the camera in a circle.

这篇关于Unity相机旋转时保持透视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆