2017-02-20 43 views
0

對於3D第一人稱控制器遊戲,我將轉換屏幕上的方向矢量。 一個對象被射向這個方向。 我的相機可以根據虛擬遊戲杆的輸入進行旋轉。 當我不旋轉並使用輕掃拍攝物體時,它會朝正確的方向行進。 但是,當我旋轉照相機時,它不會朝着預期的方向行進。 方向應適應相機的旋轉。我該如何糾正我的矢量方向到相機的旋轉?

如何糾正我的向量到相機的旋轉方向?

PS:消息我進一步澄清

//Converting swipe direction to 3D direction 
public class TouchPair 
{ 
    public Vector2 startPos; 
    public int fingerId; 
} 

private TouchPair touchPair; 

void Update() 
{ 
    foreach (Touch touch in Input.touches) 
    { 
     Vector2 touchPos = touch.position; 

     if (touch.phase == TouchPhase.Began) 
     { 
     Ray ray = cam.ScreenPointToRay(touchPos); 
     RaycastHit hit; 

     if (Physics.Raycast(ray, out hit)) 
     { 
      //The player is wielding a bomb that is visible on the screen. 
      //only swipes that start from this object should count 
      if (hit.transform.tag == "Bomb") 
      { 
       touchPair = new TouchPair(); 
       touchPair.startPos = touchPos; 
       touchPair.fingerId = touch.fingerId; 
      } 
      } 
     } 
     else if (touch.phase == TouchPhase.Ended) 
     {   
     if (touchPair.fingerId == touch.fingerId) 
     { 
      Vector2 endPos = touchPos; 
      Vector2 swipeDirectionRaw = endPos - touchPair.startPos; 
      float magnitude = swipeDirectionRaw.magnitude; 
      if (magnitude >= minSwipeLength) 
      { 
        BombController BombController =               GameObject.FindWithTag("Bomb").GetComponent<BombController>(); 
          BombController.Throw(swipeDirectionRaw.normalized, magnitude); 
      } 
     } 
     } 
    } 
}  

public void Throw(Vector2 direction, float magnitude) 
{ 

    //Setup variables for throw 
    throwDirection = new Vector3(direction.x, 0.0f, direction.y); 
    throwSpeed = magnitude * throwForce; 
}    
+0

「我正在轉換方向矢量在屏幕上的滑動。」 - 你能告訴我們這樣做的代碼嗎?我假設如果你使用['Camera.ScreenToWorldPoint'](https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html),那麼你就不會有問題了。 – Quantic

回答

0

我必須得到相機的視圖向量。通過獲取玩家的前進角度和相機的視角矢量之間的角度數量 ,接收穫得正確的方向矢量所需的角度數量。 最後通過這個角度旋轉拍攝方向。

    Vector2 endPos = touchPos; 
        Vector2 swipeDirectionRaw = endPos - touchPair.startPos; 
        float magnitude = swipeDirectionRaw.magnitude; 
        swipeDirectionRaw.Normalize(); 
        if (magnitude >= minSwipeLength) 
        { 
         GameObject player = GameObject.FindWithTag("Player"); 
         Vector3 shootOrigin = player.transform.position; 
         Vector3 uncorrectedShootDirection = new Vector3(swipeDirectionRaw.x, 0.0f, swipeDirectionRaw.y); 

         Vector3 originVector = player.transform.forward; 
         Vector3 viewVector = cam.transform.forward; 

         //Shoot direction gets corrected by angle between origin- and view vector 
         float angleBetweenOriginAndView = Vector3.Angle(originVector, viewVector); 

       //There is no clockwise or counter-clockwise in 3d space, 
       //hence mirroring is needed. In my case it's done to what suits my needs 
          if (viewVector.x < 0.0f) 
          { 
           angleBetweenOriginAndView *= -1f; 
          } 

         Vector3 correctedShootDirection = Quaternion.Euler(0, angleBetweenOriginAndView, 0) * uncorrectedShootDirection; 
        } 

        touchPair = null; 
       } 
0

您與地面光線投射需要時touch.phase == TouchPhase.Ended然後從你的性格raycast.hit獲取路線。

Raycasthit hitInfo; 
Physic.Raycast; 
相關問題