x


Printing Distance b/w two points using line renderer and Raycast

Hi guys, I want to create a "Measure Gun" which fires a laser beam and then prints the distance between the gun point and the hit point. Heres my code

if(Input.GetButton("Fire1")){
       if(Physics.Raycast(transform.position,Vector3.forward,hit,Mathf.Infinity)){

         lineRenderer.enabled = true;
         lineRenderer.SetPosition(0, transform.position);
         lineRenderer.SetPosition(1, hit.point);
         print("Distance = " + Vector3.Distance(transform.position, hit.point));
       }
    } 

I attached this script with the gunPoint(gameobject from which laser is fired). But the problem here is that the other end of the laser beam gets snapped to certain points.. The gunPoint end is fine but the other end is not smooth...

more ▼

asked Dec 27 '12 at 07:45 AM

Leon.S.Kennedy gravatar image

Leon.S.Kennedy
7 4 4 5

It sounds like it's hitting something - do you have colliders that are invisible in the way?

Dec 27 '12 at 07:47 AM whydoidoit
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Oh ya, Thanks man, now i see i have an invisible rogue collider in the middle of the scene for no reason. dont know why i created in the first place.. After deleting it, the code works fine.. Also i got rid of hit.point and used hit.distance and now everything works as intended... Thank u very much man..

PS: I love your site www.unitygems.com... great work man..

more ▼

answered Dec 27 '12 at 10:10 AM

Leon.S.Kennedy gravatar image

Leon.S.Kennedy
7 4 4 5

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2406
x422
x177
x75
x17

asked: Dec 27 '12 at 07:45 AM

Seen: 853 times

Last Updated: Dec 27 '12 at 10:11 AM