如何更正矢量旋转相机的方向?

时间:2017-02-20 19:22:19

标签: c# unity3d

对于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;
}               

2 个答案:

答案 0 :(得分:0)

当touch.phase == TouchPhase.Ended时,您需要使用地面进行光线投射,然后从角色到raycast.hit获取方向。

Raycasthit hitInfo;
Physic.Raycast;

答案 1 :(得分: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;
                }