Unity Line渲染器不遵循Raycast

时间:2015-09-30 08:01:21

标签: c# unity3d

我目前在制作线条渲染器时遇到问题,就像我的光线投射一样。 here是我发现用于反映我的光线投射的来源 这是我的代码:

/clamp the number of reflections between 1 and int capacity  
    nReflections = Mathf.Clamp(nReflections,1,nReflections);  
    //cast a new ray forward, from the current attached game object position  
    ray = new Ray(goTransform.position,goTransform.right);  
    //      Debug.DrawRay(hit.point, inDirection*100, Color.magenta);  

    //represent the ray using a line that can only be viewed at the scene tab  
    Debug.DrawRay(goTransform.position,goTransform.right * 100, Color.magenta);  

    //set the number of points to be the same as the number of reflections  
    nPoints = nReflections;  
    //make the lineRenderer have nPoints  
    lineRenderer.SetVertexCount(nPoints);  
    //Set the first point of the line at the current attached game object position  
    lineRenderer.SetPosition(0,goTransform.position);  

    for(int i=0;i<=nReflections;i++)  
    {  
        //If the ray hasn't reflected yet  
        if(i==0)  
        {  
            //Check if the ray has hit something  
            if(Physics.Raycast(ray.origin,ray.direction, out hit, 100)&& ( hit.transform.name != "Ceiling"))//cast the ray 100 units at the specified direction  
            {  

                //the reflection direction is the reflection of the current ray direction flipped at the hit normal  
                inDirection = Vector3.Reflect(ray.direction,hit.normal);  

                ray  = new Ray(hit.point,inDirection);

                //cast the reflected ray, using the hit point as the origin and the reflected direction as the direction 
                //                  nReflections = nReflections - 1;

                Debug.DrawRay(hit.point, hit.normal*3, Color.blue);  
                //represent the ray using a line that can only be viewed at the scene tab  
                //Draw the normal - can only be seen at the Scene tab, for debugging purposes  


                //Print the name of the object the cast ray has hit, at the console  
                Debug.DrawRay(hit.point, inDirection*100, Color.magenta);  

                //                  if (hit.transform.tag){
                //Debug.Log("Object name: " + hit2d.transform.tag);
                //
                //                  }

                //if the number of reflections is set to 1  
                if(nReflections==1)  
                {  
                    //add a new vertex to the line renderer  
                    lineRenderer.SetVertexCount(++nPoints);  
                }  

                //set the position of the next vertex at the line renderer to be the same as the hit point  
                lineRenderer.SetPosition(i+1,hit.point); 

                //                  lineRenderer.SetWidth(0,1);
            }  else {
                if(nReflections==1)  
                {  
                    //add a new vertex to the line renderer  
                    lineRenderer.SetVertexCount(++nPoints);  
                } 
                lineRenderer.SetPosition(i+1,hit.point); 

            }
        }  
        else // the ray has reflected at least once  
        {  
            //Check if the ray has hit something  
            if(Physics.Raycast(ray.origin,ray.direction, out hit, 100)&& ( hit.transform.name != "Ceiling"))//cast the ray 100 units at the specified direction  
            {  
                //the refletion direction is the reflection of the ray's direction at the hit normal  
                inDirection = Vector3.Reflect(inDirection,hit.normal);  

                ray  = new Ray(hit.point,inDirection);

                Debug.DrawRay(hit.point, inDirection*100, Color.magenta);  

                //Print the name of the object the cast ray has hit, at the console  
                //                  Debug.Log("Object name: " + hit.transform.tag);  

                //add a new vertex to the line renderer  

                lineRenderer.SetVertexCount(++nPoints);  
                //set the position of the next vertex at the line renderer to be the same as the hit point  
                lineRenderer.SetPosition(i+1,hit.point);  

                //                  lineRenderer.SetWidth(0,1);
            }  else {

                lineRenderer.SetVertexCount(++nPoints); 
                lineRenderer.SetPosition(i+1,hit.point); 

            }
        }  
    }  

对于第一次迭代,该线匹配我想要的线,但是当我将1作为我的反射数时,它返回给我2个反射。当我放2时,它会给我3个反射。当我放3个或更多时,它仍然会做同样的,但这次它给了我多余的线点,并且多余的给了我一个未知的位置。

2 个答案:

答案 0 :(得分:1)

迭代从0开始,因此当您编写i<=nReflectionsnReflections3时,它将迭代为0123因此,共计4次。这就是为什么你应该把它写成i<nReflections

答案 1 :(得分:1)

试试这个,它应该做你想要的。

            RaycastHit hit;
            int hits = 0;
            Vector3 direction = transform.forward;  
            Vector3 lastHitPosition = transform.position;

            lineRenderer.SetVertexCount(2);
            lineRenderer.SetPosition(0, transform.position);

            while(hits < nReflections){
                if(Physics.Raycast(lastHitPosition, direction, out hit, 100)){
                    hits++;
                    lineRenderer.SetVertexCount(hits+2);
                    lineRenderer.SetPosition (hits,hit.point);

                    direction = Vector3.Reflect(direction, hit.normal);
                    lastHitPosition = hit.point;
                }else break;
            }
            lineRenderer.SetPosition(hits+1, lastHitPosition+direction*100);