Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于对象方向和滑动手势的光线投射_C#_Unity3d_Rotation_Raycasting - Fatal编程技术网

C# 基于对象方向和滑动手势的光线投射

C# 基于对象方向和滑动手势的光线投射,c#,unity3d,rotation,raycasting,C#,Unity3d,Rotation,Raycasting,在我的游戏中,当玩家在4个方向中的任意一个方向上滑动时,将根据该方向从对象创建一条光线,然后对象将朝该方向旋转: void Update () { if (direction == SwipeDirection.Up) { RayTest (transform.forward) transform.rotation = Quaternion.LookRotation (Vector3.forward); } if (direct

在我的游戏中,当玩家在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);
}
如果每次滑动后我都不旋转物体,那么代码就完美了,旋转它会弄乱方向,因此如果玩家向上滑动并且物体向右看,光线方向就会变成它的前进方向,这是正确的


如何解决此问题?

您正在指定相对于变换的光线方向,方法是说,即
transform.right
。这意味着旋转变换后,
transform.right
的含义与以前不同

您没有指定摄影机是否随播放机旋转,因此我假设它不随播放机旋转。在这种假设下,
SwipeDirection.Right
始终表示相同的方向,因此
RayTest(…)
也应始终测试相同的方向

所以我想你只需要一个恒定的方向,比如
Vector3。正确的
作为
RayTest(…)
的参数。

是的,用“Vector3”更改“transform”确实做到了。非常感谢。