2017-02-20 5 views
0

3D初心者のコントローラのゲームでは、画面上のスワイプを方向ベクトルに変換しています。 オブジェクトがこの方向に射撃されます。 私のカメラは、仮想ジョイスティックの入力に基づいて回転できます。 スワイプを使用してオブジェクトを回転させたり撮影したりしないと、正しい方向に移動します。 しかし、カメラを回転させると、意図した方向には移動しません。 方向はカメラの回転に合わせる必要があります。ベクトルの向きをカメラの回転に合わせるにはどうすればよいですか?

ベクターのカメラの回転方向を修正するにはどうすればよいですか?

PS:touch.phaseは== TouchPhase.Endedその後、raycast.hitためにあなたのキャラクターからの方向を取得するときに、さらに明確化

//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

のメッセージ私は、あなたは地面にレイキャストが必要です。

Raycasthit hitInfo; 
Physic.Raycast; 
関連する問題