Raycast shooting

I'm not that new to Unity but I am new to raycasts. I want to make a gun (my gun is attached to the camera of the first person shooter gameobject) use a raycast to shoot instead of a rigidbody. I also don't know how to draw a visible line and sort of an explosion-type thing for visual effects.

i ConnE's recommendation is a good one. It is simple and doesn't require too much work.

If you want to create your own solution, this is fairly well-documented in the docs, forums and other questions. Also, there are several questions that you're asking and they should have been split up: How do I use raycast? How do I draw a line? How do I create an explosion?


See the script reference for raycast which has example code. This is the physics raycast, but there are other variants which may better apply to your use case depending on what you want to collide with.

Calculate your gun's direction, the point your gun is firing from and the distance it can fire, then call `Raycast(gunOrigin, gunDirection, out hitInfo, gunRange, layerMask)`. Using the hitInfo.collider or hitInfo.rigidbody, you will know what was hit.

Drawing Lines

What do you mean by a visible line? This is well documented regardless of your meaning in the answers and forums.

If you're looking for information in the scene view about your raycast, you could use Debug.DrawLine.

If you're looking for a line in 3D space in the game view, you might consider using paricles (see Line Renderer or Trail Renderer).

There are several other ways to achieve this (both good and bad) and it really depends on the kind of visible line that you want. You could write an image effect with a shader to generate your line. You could instantiate geometry(plane) to represent the line and ensure that it is always facing the camera. You could create the line as part of the GUI (probably not what you want, but still a valid solution to the question as posted).

Sort of an explosion-type thing

What? This is very subjective and ambiguous. This is also well documented in tutorials, answers and the forums, regardless of your meaning.

A common solution is to do this with particles.

It really depends on your use case. You could create an animated mesh. You could do this as an image effect with a shader to generate your explosion. You could do this with a camera-facing plane that has an animated texture. You could even make it part of the GUI (probably not what you want, but still a valid solution to the question as posted).

See this http://www.dastardlybanana.com/download.php?download_file=DBWeaponsPackage0.92.unitypackage And u can past your gun in that scene ... its very easy weapon package based on raycasting :P and for the visible line i dont know

Cheers :D

if you want a line, to mimic a bullet path (light), all u need to do is create an empty gameobject then, component > miscellaneous > line renderer u should now have a gameobject with a line renderer(you probably wont see the line right now)

tweaking the settings slightly:

u want to change the material element in the inspector of the line renderer, to fire add(preset material it does the job)

in positions u can have as many as u want if u change the size to a desired number, 2 is good. u only need to change the z length of all of them make sure that element 2's z size > element 1 and element 3 > 2 or else ur line will not look proper. My element 1 is size 2 and element 2 is size 5.

make ur widths as desired (should be self explanatory)


now with the code:

sorry but i only know properly how to do it in c#, javascript is easy to convert but i wont know if it will work.


public LineRenderer line;

 //line renderer will automatically be disabled on game play as this is
 already its settings for some reason

  //when a key is pressed do something
     //enable the line
       line.renderer.enabled = true;
       Ray ray = Camera.main.ScreenPointToRay(transform.position);
       RaycastHit hit = new RaycastHit();
       if (Physics.Raycast(ray, out hit))
      {     /*example line>>>> if (hit.collider.gameObject.tag("enemy")

                   Destroy(<<the enemy>>)
                }                              <<<<example code*/
          //if nothing is being pressed then set the line back to false
            line.renderer.eanbled = false;

hope this helps a little but it should give u and guide u in the right direction :D