首页 文章

使用滑动手势基于对象方向的Raycast

提问于
浏览
2

在我的游戏中,当玩家在4个方向中的任何一个方向上滑动时,基于该方向从对象创建光线,然后对象朝该方向旋转:

void Update ()
{
    if (direction == SwipeDirection.Up)
    {
        RayTest (transform.forward)

        transform.rotation = Quaternion.LookRotation (Vector3.forward);
    }

    if (direction == SwipeDirection.Right)
    {
        RayTest (transform.right)

        transform.rotation = Quaternion.LookRotation (Vector3.right);           
    }

    if (direction == SwipeDirection.Down)
    {
        RayTest (-transform.forward)

        transform.rotation = Quaternion.LookRotation (Vector3.back);            
    }

    if (direction == SwipeDirection.Left)
    {
        RayTest (-transform.right)

        transform.rotation = Quaternion.LookRotation (Vector3.left);        
    }
}

void RayTest (Vector3 t)
{
    // "transform.position + Vector3.up" because the pivot point is at the bottom
    // Ray is rotated -45 degrees
    Ray ray  = new Ray(transform.position + Vector3.up , (t - transform.up).normalized);
    Debug.DrawRay (ray.origin, ray.direction, Color.green, 1);
}

如果我在每次滑动后不旋转对象,旋转它会使方向变得混乱,则代码工作得很完美,因此如果玩家向上滑动并且对象向右看,则光线方向变为向前方向,这是正确的 .

我怎么解决这个问题?

1 回答

  • 0

    您正在通过说_1148723_指定相对于变换的光线方向 . 这意味着一旦你旋转你的变换, transform.right 的含义就不再像以前那样了 .

    你没有't specify whether the camera rotates with the player, so I am assuming it doesn' . 在这个假设下, SwipeDirection.Right 总是意味着相同的方向,所以 RayTest(..) 也应该始终测试相同的方向 .

    所以我想你只需要一个像 Vector3.right 这样的常量方向作为 RayTest(..) 的参数 .

相关问题