- Home /
 
 
               Question by 
               Bincredible · Oct 27, 2013 at 10:53 PM · 
                raycastlinerendererdetectionhit  
              
 
              Help with LineRenderer
Hey, I wondered if there was a way to detect if the LineRenderer component is touching another object for example a cube through a script. Thanks for any help.
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by vexe · Oct 28, 2013 at 02:56 PM
Just cast a ray of the same length and direction of your line renderer.
Here's an example (just attach it to your desired gameObject):
 public class Test : MonoBehaviour
 {
     public float length = 10;
     LineRenderer line;
     Transform mTransform; // cached transform
     void Start()
     {
         line = GetComponent<LineRenderer>();
         if (!line)
            line = gameObject.AddComponent<LineRenderer>();
         mTransform = transform;
         line.SetVertexCount(2);
         line.SetWidth(.1f, .1f);
     }
     void Update()
     {
         // set the positions of the line's two vertices
         line.SetPosition(0, mTransform.position);
         line.SetPosition(1, mTransform.position + mTransform.forward * length);
         // create a cast a ray from our position, going forward and of length 'length'
         Ray ray = new Ray(mTransform.position, mTransform.forward);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, length)) {
            print(hit.collider.name);
         }
     }
 }
 
               Result:

 
                 
                archery.png 
                (79.4 kB) 
               
 
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with casting and drawing multiple raycast along an angle 0 Answers
I don't know why i can't detect by ray sth. tagged, 1 Answer
hit point not working correctly . 1 Answer
Trigger and raycast error? 2 Answers