【问题标题】:Unity Line Renderer not following RaycastUnity Line Renderer 不遵循 Raycast
【发布时间】:2015-10-01 01:01:30
【问题描述】:

我目前在使我的线渲染器完全按照我的光线投射时遇到问题。 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 个或更多时,它仍然做同样的事情,但这次它给了我多余的线点,而多余的给了我一个未知的位置。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    迭代从0开始,所以当你写i&lt;=nReflectionsnReflections3时,它将迭代为0123,因此,总共4次.这就是为什么你应该把它写成i&lt;nReflections

    【讨论】:

      【解决方案2】:

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

                  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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多