Search Unity

How to do a laser weapon

Discussion in 'Scripting' started by stuthemoo, Oct 31, 2011.

  1. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Hi, I'm making a top down space shooter like the retro 'Asteroids' game and I want to add a laser weapon. Sort of like the attached image.

    But I'm not sure how I should go about it. I'm guessing Rays? But I've never used them before, could someone please give me an idea of how to go about it.

    Thanks from,
    Stuart.
     

    Attached Files:

    • $1.png
      $1.png
      File size:
      119.3 KB
      Views:
      1,622
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    The laser would be a thin cylinder - I think thats probably best with a decent shader. The sparks are merely a particle system oriented to the normal of the surface.

    Things to search for:

    1. align mesh to ground / super mario galaxy (this will allow you to transform the collision point of the ray to the hit point)
    2. spawn unity particle system at that alignment.

    I'm sorry I don't have time to provide code, but those are useful starting points.


    edit:

    You don't actually need funky math...
    1. use raycast to get the point where it collides
    2. use this point and the ship's point to position the beam object halfway between the two
    3. use HALF the distance between the 2 to scale it to be the correct size
    4. spawn the sparks 180 degrees away from the rotation of the actual ship. this isn't 100% accurate but may be good enough for your purposes.
     
    Last edited: Oct 31, 2011
  3. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Thanks for the quick reply, I'll research those now.
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    just for clarification a ray is invisible and only for detection purpose. as visual representation you could also use the linerenderer or eric's vectrosity package (asset store).

    if your laser goes straight ahead from the emitting ship cast a ray in the foward direction and see if it hits something (collider required). if so draw the beam (linerednerer) from start to end point and put a particle emitter there (or have one all the time and just activate and reposition it). for more information and examples look in the docu.
     
  5. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Hi Guys,

    I've had a go this evening and I've managed to inflict damage to the opponent when the ray hits them and also show the laser using lineRenderer. The only problem is that I have boundaries on my level which are triggers, when they are hit by the ray they return values to my RaycastHit.point which I am using for the end of the line renderer and this is making my laser point in strange directions when I shoot the triggers (because I have "lineRenderer.useWorldSpace = false").

    I hope that makes sense. Is there anyway I can make my ray ignore triggers and only collide with non trigger colliders?

    Thanks,
    Stuart
     
    Last edited: Nov 3, 2011
  6. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    YAY I figured it out by using "lineRenderer.SetPosition(1, Vector3(0,0,hit.distance))" instead of "lineRenderer.SetPosition(1, hit.point)" to draw the end of my line. How simple :D
     
    Last edited: Nov 3, 2011
  7. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Ok, now I want to add the particle emitter and also some nice graphics for the laser. How can I spawn particles at the point the ray hit? Also how would you make the laser appear like a laser? Can I give it some kind of glow?

    Thanks,
    Stuart
     
  8. ivkoni

    ivkoni

    Joined:
    Jan 26, 2009
    Posts:
    978
    angry bots has a pretty good laser if you haven't noticed.
     
  9. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Just had a look and yeah it looks really good. Do you know how they did the graphics for it?