2016-07-23 106 views
1

目前,我有以下两个问题:复位位置相对于旋转

  • 的第一个问题是,当我恢复我的相机位置我也必须重新设置我的相机旋转。这是因为我的相机 偏移量设置为与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; 

} 

Image displaying what I mean

+0

你能解释多一点你真的想让相机做什么?我想我会得到你想围绕一个点旋转,并根据需要重置当前面对的球员,但请确认。 – Absinthe

+0

是的,一个截图或绘图(甚至程序员艺术)将帮助很多 – 2016-07-23 19:36:11

+0

@Absinthe是的,很抱歉,基本上它是我想旋转相机在屏幕的中间,所以基本上它围绕着它旋转看完全,我目前的代码没有问题,但它的焦点在旋转时略微移动,它似乎是一种昂贵的计算方法。至于重置旋转部分,我希望将我的相机重置为已旋转的方式,尽管它的定位方式可以看到我的角色。希望这可以清除它。谢谢! –

回答

0

使用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); 
} 
+0

问题与该代码是我不知道我的距离,因此,raycast,也不回答我的问题的第一和主要部分。 –