纳金网

标题: unity3d中创建激光束,How to do Lasers in Unity3D [打印本页]

作者: 晃晃    时间: 2011-10-14 08:35
标题: unity3d中创建激光束,How to do Lasers in Unity3D


           I was recently asked about how the lasers were done in Star Wars Trench Run and I’d said I’d blog about that and a few other items, so here’s the first of a few.  This is the same material I covered at Unite 2009, so if you’ve seen the video or were there, there’s nothing new;)
         

           Here we’ll cover one way of doing lasers with Unity3D that looks good and is performance friendly for the iPhone.  This isn’t meant to be exhaustive by the way, it’s just meant to be the base overview of what a laser *is* in Unity and how I managed it in terms of performance for the iPhone game.  So with that, let’s jump in to some steps on setting it up.
         

           Create LineRenderer
         

           0. First, Create an empty GameObject in your scene.
         

           1.  Then, attach a line renderer component to your GameObject and position the GameObject where the tip of the laser cannon will fire from.  Why not just use a particle emitter?  This is because the ellipsoid particle emitter has a bug.  even if you set the size of the ellipsoid to 0 on all 3 axis or .0001, you will see that it starts at a random location on the z axis.  Unity acknowledged the bug.  So to fix this, since we can’t emit using the particle emitter from the cannon’s tip, we have to first show a line renderer.  Below are some typical settings you could use for a LineRenderer as a laser bolt:
         


           Properties of a general LineRenderer for a laser
         

           Here’s what it should look like in the Unity IDE when you get your GameObjects in place:
         


           GameObjects with LineRenderes placed at the cannons
         

            
         

           I created a class called LaserControl, and when I want it to show, this is literally what I do:
         
          public var isShowingLaser:boolean = false; function showLaser() {   if( isShowingLaser ) return; isShowingLaser = ***e; thisRenderer.enabled = ***e; yield WaitForSeconds (.05); resetLaser(); isShowingLaser = false; }
         
           as you can see, it shows it for .05ms then hides it by calling reset laser which just does this:
         
          function resetLaser() {     this.renderer.enabled = false; }
         
           3.  So, then create your own class that extends MonoBehaviour and attach it to the GameObject that you added the LineRenderer to.
         

           Create Particle Emitter
         

           Now that we have this really nice LineRenderer laser that looks great coming from the tip of the cannon, we actually need to emit a laser to “fly” on it’s own path, right? So the concept for this is:  create empty GameObjects to server as 3D points for your emitter to “jump” to when you when it is to emit a laser from a particular cannon.  Since a particle emitter counts as 1 draw call for all of its particles, this makes it especially nice on an iPhone build.  Even better, it’s very lightweight on a web based game.
         

           0.  Create 1 new GameObject for every cannon you have.  In this instance, I had 4 for the X-Wing, so I created 4 GameObjects with nothing in them.  Then give them names like TL, TR, BL, BR (topLeft, topRight, bottomLeft, bottomRight).
         

           Here’s a screenshot of one of the emitter locations highlighted in the editor:
         




           Empty GameObject serves as location for emitter later on
         

           1.  Position these new GameObjects at the very end of the LineRenderer in your scene.  These will be the location that the emitter jumps to just before being told to Emit(1).
         

           2.  Now, create a GameObject.  Then, with that GameOject selected, add: 1) Ellipsoid Particle Emitter, 2) Particle Animator, 3) Particle Renderer.
         

           3.  In Particle Ellipsoid Emitter, set Local Velocity > z = -500.  This is the speed essentially.  If you want a faster laser, increase the number.  Make sure “Simulate in worldspace” is checked as this will allow emitted particles to carry on their path in world space once they’re fired.  Set the Ellipsoid to 0 on all 3 axis.  Finally, set Min Emitter Range = .0001 to make sure it fires from the closest location to the center of the GameObject as possible.  Like I said, with the bug, it’s a bit random on the z axis, but you’ll never see that in the game.  Leave the Particle Animator as is.
         

           4.  Now the other bit of magic in making the laser is the Particle Renderer.  You’ll want to set “Stretch Patricles” = Stretched, with a length scale of 7 or so.  You can play with this number as it will determine how good your bolt looks in length.  Then set the “Max Particle Size” = .01.
         

           Here’s a screen shot of the settings for the particle emitter and renderer ( the animator is left with its defaults ):
         


           Particle Emitter settings
         

           Conclusion
         

           At this point, you have the physical objects in your scene to manage your lasers.  The last remaining step is to write a class that moves the emitter to each of those 4 emitter location GameObjects, tells the LineRenderer to show() and then emit 1 particle – BAM!  You now have lasers in Unity!
         

           Hope this helps and hope you have a Bandit day
         

作者: tc    时间: 2012-3-1 04:19
谢谢楼主,真是太实用了

作者: 晃晃    时间: 2012-4-11 23:22
此地無銀。。。

作者: markq    时间: 2012-4-12 22:48
不错 非常经典 实用
作者: tc    时间: 2012-8-20 23:28
心中有爱,爱咋咋地

作者: nts    时间: 2013-10-17 10:37
强大无比的教程
作者: Chubbaz    时间: 2014-3-9 16:25
Thanks for sharing.




欢迎光临 纳金网 (http://rs.narkii.com/club/) Powered by Discuz! X2.5