目前我有以下两个问题:
-
第一个问题是,当我重置相机位置时,我还必须重置相机旋转。这是因为我的相机偏移被设置为在z和y方向上稍微偏离我的球员的位置轴,显然这些值应该根据我的相机而变化尽管我不确定如何计算出这些值应该是。
-
我的第二个问题是,我的旋转使用光线投射来找到屏幕并确定其旋转原点,尽管它看起来是稍微偏离屏幕中间,就像旋转时一样原点也会移动,如果它确实在屏幕的中间它不应该完全静止吗?还有更好的和更少的吗实现我想要的轮换的昂贵方式?
相关代码:
void RotateCamera()
{
//Find midle of screen
Ray ray = Camera.main.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
RaycastHit hitInfo;
//Checks if ray hit something
if (Physics.Raycast(ray, out hitInfo))
{
//Rotate left and right
if (Input.GetKey(KeyCode.RightArrow))
{
transform.RotateAround(hitInfo.point, -Vector3.up, rotationSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.RotateAround(hitInfo.point, Vector3.up, rotationSpeed * Time.deltaTime);
}
}
//Draws Raycast
Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow);
}
void ResetCameraPosition()
{
//Reset and lock camera position
transform.rotation = Quaternion.identity;
transform.position = player.transform.position + cameraOffset;
}
图像显示
使用Camera.ScreenToWorldPoint在屏幕中间创建一个"目标",围绕该目标进行枢轴旋转。删除所有光线投射的东西,因为你不需要它,并用替换相关的部分
float rotationSpeed = 45; // or whatever speed
float distance = 5f; // or whatever radius for the orbit
Vector3 target = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, distance));
if (Input.GetKey(KeyCode.RightArrow))
{
transform.RotateAround(target , -Vector3.up, rotationSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.RotateAround(target , Vector3.up, rotationSpeed * Time.deltaTime);
}