c# - Unity Line Renderer not following Raycast -


i'm having problem making line renderer ray cast. here source i've found reflecting raycast , here code:

/clamp number of reflections between 1 , int capacity       nreflections = mathf.clamp(nreflections,1,nreflections);       //cast new ray forward, current attached game object position       ray = new ray(gotransform.position,gotransform.right);       //      debug.drawray(hit.point, indirection*100, color.magenta);        //represent ray using line can viewed @ scene tab       debug.drawray(gotransform.position,gotransform.right * 100, color.magenta);        //set number of points same number of reflections       npoints = nreflections;       //make linerenderer have npoints       linerenderer.setvertexcount(npoints);       //set first point of line @ current attached game object position       linerenderer.setposition(0,gotransform.position);        for(int i=0;i<=nreflections;i++)       {           //if ray hasn't reflected yet           if(i==0)           {               //check if ray has hit               if(physics.raycast(ray.origin,ray.direction, out hit, 100)&& ( hit.transform.name != "ceiling"))//cast ray 100 units @ specified direction               {                    //the reflection direction reflection of current ray direction flipped @ hit normal                   indirection = vector3.reflect(ray.direction,hit.normal);                    ray  = new ray(hit.point,indirection);                  //cast reflected ray, using hit point origin , reflected direction direction                  //                  nreflections = nreflections - 1;                  debug.drawray(hit.point, hit.normal*3, color.blue);                   //represent ray using line can viewed @ scene tab                   //draw normal - can seen @ scene tab, debugging purposes                     //print name of object cast ray has hit, @ console                   debug.drawray(hit.point, indirection*100, color.magenta);                    //                  if (hit.transform.tag){                 //debug.log("object name: " + hit2d.transform.tag);                 //                 //                  }                  //if number of reflections set 1                   if(nreflections==1)                   {                       //add new vertex line renderer                       linerenderer.setvertexcount(++npoints);                   }                    //set position of next vertex @ line renderer same hit point                   linerenderer.setposition(i+1,hit.point);                   //                  linerenderer.setwidth(0,1);             }  else {                 if(nreflections==1)                   {                       //add new vertex line renderer                       linerenderer.setvertexcount(++npoints);                   }                  linerenderer.setposition(i+1,hit.point);               }         }           else // ray has reflected @ least once           {               //check if ray has hit               if(physics.raycast(ray.origin,ray.direction, out hit, 100)&& ( hit.transform.name != "ceiling"))//cast ray 100 units @ specified direction               {                   //the refletion direction reflection of ray's direction @ hit normal                   indirection = vector3.reflect(indirection,hit.normal);                    ray  = new ray(hit.point,indirection);                  debug.drawray(hit.point, indirection*100, color.magenta);                    //print name of object cast ray has hit, @ console                   //                  debug.log("object name: " + hit.transform.tag);                    //add new vertex line renderer                    linerenderer.setvertexcount(++npoints);                   //set position of next vertex @ line renderer same hit point                   linerenderer.setposition(i+1,hit.point);                    //                  linerenderer.setwidth(0,1);             }  else {                  linerenderer.setvertexcount(++npoints);                  linerenderer.setposition(i+1,hit.point);               }         }       }   

for 1st iteration line matches desired line, when put 1 number of reflections returns me 2 reflections. , when put 2, returns me 3 reflections. when put 3 or more, still same, time gives me excess line point, , excess gives me unknown position.

try this, should want.

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

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -