在unity3d中使用标签问题进行光线投射

时间:2012-12-02 17:01:01

标签: c# unity3d raycasting

我需要一些帮助。我有一部分代码用raycast搜索未阻止的邻居。我需要得到与“WP”标签碰撞的光线投射。两个迭代都显示了正确的结果,转储和光线投射也是如此,光线投射成功地碰撞了一些东西,但是当我检查光线投影与之相撞时,没有显示结果......任何人都知道这是错误的代码??

int flag = 0, flahNeigh = 0;
    for(flag = 0; flag< wayPoints.WPList.Length; flag++) // iteration to seek neighbour nodes
            { 
                for (flagNeigh = 0; flagNeigh < wayPoints.WPList.Length; flagNeigh++)
                {                
                    if (wayPoints.WPList[flag].loc != wayPoints.WPList[flagNeigh].loc) // dump its own node location
                    {                    
                        if (Physics.Raycast(wayPoints.WPList[flag].loc.position, wayPoints.WPList[flagNeigh].loc.position, out hitted)) // raycasting to each node else its self
                        {  
                            if (hitted.collider.gameObject.CompareTag("WP")) // check if the ray only collide the node
                            {
                               print(flag + "  :  " + flagNeigh + "  :  " + wayPoints.WPList[flagNeigh].loc.position); // debugging to see whether the code works or not (the error comes)
                            }
                        }
                    }                    
                }
            }

感谢您的赞赏和回答...... 抱歉,如果我的英语不好...... ^^

1 个答案:

答案 0 :(得分:0)

这是因为你正在使用邻居的位置。只有当原点来自0,0,0时才有效。

所以不要写:

if (Physics.Raycast(wayPoints.WPList[flag].loc.position, wayPoints.WPList[flagNeigh].loc.position, out hitted))

写:

 if (Physics.Raycast(wayPoints.WPList[flag].loc.position, wayPoints.WPList[flagNeigh].loc.position-wayPoints.WPList[flag].loc.position, out hitted))