- Home /
How to get a LineRenderer to shoot from Gun Point to Mouse Position
So i created a LineRenderer to be used a a Laser Gun, it worked fine when I wanted it to shoot from the Gun Position forward but Now that I'm trying to make it go towards the Mouse it mainly just shoots upwards or in a random location. Here is my code so far.
 if(Input.GetMouseButtonDown(0)){ 
 
 
 var start = transform.position;
 laser.SetPosition(0,start);
 var location = Input.mousePosition;
 var finish = start + location * 10;
 var ray : RaycastHit;
 
 
 if(Input.GetButton("Fire1") && Physics.Raycast (location, start, ray)) 
 
 finish = ray.point;
 
 laser.SetPosition(1,finish);
Any help is much appreciated.
Answer by robertbu · Apr 01, 2014 at 01:06 AM
You need to do some research into how Raycast are done. You either cast a ray, or you can cast from a world position and direction. Your Raycast() is using a screen location for the position and a world position for the direction. The typical code would look like this:
 var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 var hit : RaycastHit;
 if(Input.GetButton("Fire1") && Physics.Raycast (ray, hit)) { 
 
     laser.SetPosition(1,hit.point);
 }
Note this is using the 'ray' version of Raycast().
Thanks, This works when shooting the floor of my $$anonymous$$aze however when I shoot the walls the laser appears like a blur in the middle of the screen, Not starting at the start point and not finishing where the mouse is clicked.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                