Show "projectile" with raycasting?

Hi,
I’m making a simple 3d space shooter. I got raycasting working but I was wondering if it was possible to show some little laser beams along the raycasting path. I understand that raycasting is instantanious but is there anyway I can add a visual effect on the ray’s path?

Thank you for your time!

Look up LineRenderer in the documentation.

hi. Don’t use 1000 for raycast length, if you don’t know your max distance, simply use Mathf.Infinity instead of 1000. That will shoot infinite ray.

in my signature, test2 (i believe) there’s rats, shooting line renderer on their path.
Here’s the code i am using to get you started:

//Line renderer color and length (keep it low)
var c1 : Color = Color.red;
var c2 : Color = Color.red;
var lengthOfLineRenderer : int = 20;

function Start() {
	//declare renderer
     var lineRenderer : LineRenderer = gameObject.AddComponent(LineRenderer);
     lineRenderer.material = new Material (Shader.Find("Particles/Additive"));
     lineRenderer.SetColors(c1, c2);
     lineRenderer.SetWidth(0.1,0.1);
     lineRenderer.SetVertexCount(lengthOfLineRenderer);
	 }
	 
function Shooting () {
		var forward = transform.TransformDirection(Vector3.forward);
		var hitInfo : RaycastHit;
		//shoot infinite ray
if (Physics.Raycast (transform.position, forward, hitInfo, Mathf.Infinity)) {
		// render raycast path
		var lineRenderer : LineRenderer = GetComponent(LineRenderer);
      			lineRenderer.SetPosition(0, transform.position);
      			lineRenderer.SetPosition(1, hitInfo.transform.position);
	}
}

post a screenie, if it worked :slight_smile:

Thanks guys. That helped me. It’s what I asked for but now I understand that what I asked for is not what I was looking for! hehe!
I’m trying to make a simple 3d shooter (space invaders style).
What would be the best way to handle shooting?

Thank you for your time.

there are 2 ways of doing it - raycasting for instant damage and colliding instantiated bullet with a trigger collider. Your choice
Also to avoid spamming shoot button i’d suggest you using Update() function or InvokeRepeating inside of it to control execution of your Shooting() function

there are also many ready scripts that can be found here on forums and scattered all around web, including such sites as unifycommunity, unityanswers and many unity tutorial sites found on google