查看: 6862|回复: 6
打印 上一主题 下一主题

[经验分享] unity3d中创建激光束,How to do Lasers in Unity3D

[复制链接]

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

跳转到指定楼层
楼主
发表于 2011-10-14 08:35:00 |只看该作者 |倒序浏览


           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
         
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2012-3-1 04:19:29 |只看该作者
谢谢楼主,真是太实用了
回复

使用道具 举报

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

板凳
发表于 2012-4-11 23:22:53 |只看该作者
此地無銀。。。
回复

使用道具 举报

markq    

511

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
15839
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-4-12 22:48:47 |只看该作者
不错 非常经典 实用
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-8-20 23:28:59 |只看该作者
心中有爱,爱咋咋地
回复

使用道具 举报

nts    

3

主题

1

听众

743

积分

初级设计师

Rank: 3Rank: 3

纳金币
7
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2013-10-17 10:37:08 |只看该作者
强大无比的教程
回复

使用道具 举报

23

主题

0

听众

1102

积分

助理设计师

Rank: 4

纳金币
1549
精华
0

活跃会员

7#
发表于 2014-3-9 16:25:13 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-11-16 04:17 , Processed in 0.093012 second(s), 27 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部