Shooting Lasers

I’m making a 3D space sim. What I need to know is how to create a laser, fire it, and make it explode when colliding with it’s target. I’ve been looking around for a week now I can’t find any scripts that work, even when I modify. Is there script that can help me? Thanks is advance

There’s a script that can help you! In fact, there is one that does exactly, to the letter, what you want. But it doesn’t exist yet, because, err… You haven’t written it yet. :wink:

Poor humor aside, the tool you need to model your laser is the LineRenderer. Look into that, and then study the procedural example projects here:

http://unity3d.com/support/resources/example-projects/procedural-examples.html

Your question asks for a little much to begin with - firing something, drawing a laser, colliding with something, making an explosion, those are all different pieces of functionality, and the code that makes it work isn’t even one script. Ideally, you’d split that stuff into several classes. So you gotta break your problem down, and tone down your expectations a little in the beginning. Start by seeing if you can draw something with the LineRenderer. Anything. Then if you can’t come back and present your issues with it, and you can get help with that. Then when you understand how the LineRenderer works, start working on how to direct it from the player character in whatever direction he’s firing. Then if you have problems with that, come back for help, etc, etc. etc.

The essence of what I’m saying is:

  1. Inspiration is fine. But you have to
    write your script yourself.

  2. Define milestones, then accomplish
    those,
    step by step.

I guess I am in a “Very Giving Mood”

What you need to do is:

  1. Model of Laser (Capsule is what worked for me, scale it by One axis until it looks like a laser)

  2. Texture the model - Create a new Material, then assign the Default color to Red, then drag it onto the Laser model

  3. Create a prefab with the Model - Create a new prefab, then drag the Laser model onto it.

  4. Add script that makes it go forward: New JavaScript → add these lines:

            function Update() {
               transform.Translate(Vector3.forward * Time.deltaTime);
            }
    
  1. add that script to your Prefab

Thats how you can create a laser. Later when I have more time i’ll update for how to Fire the laser.

Ok. Now my problems have changed. All my “enemies” have damage receivers. Now, I need the lasers to damage to an object with a damage receiver. I also need to change the script where you can hold the button down and lasers shoot out continually. How should I write that in this script:

var Laser : Transform;
function Update () 
{
    if(Input.GetButtonDown("Fire1"))
    {
        Instantiate(Laser, transform.position, transform.rotation);
    }
}