平台游戏进度条
这是最近你们很多人都希望我做的——一个关于如何在游戏进程中追踪角色进度的unity教程。
为了在一个平台游戏中保持玩家的注意力,你可能想要向他们展示一些作为他们游戏进度指示的HUD条。这里是一个使用GUI命令和一个纹理来实现这个功能的例子,跟往常一样,受网站宽度的限制,脚本在这里提供——http://www.pasteit4me.com/2269002(这个地址好像显示不出来,代码下面有==!)
第一步
在这个例子中,我们简单地使用两个对象作为起始位置——他们可以使空对象或者碰撞器来停止正在运动的角色,这取决于你。记住,这个例子只是针对二维层次的进度追踪设计的,所以没有将高度考虑进去。
第二步
编写下面的脚本并将它赋予到一个空物体上。在脚本组件的监视器中设定你想要的进度条宽度和高度。然后将角色拖动到角色变量的位置,将起始位置的对象也拖到相应的这些变量上(这些变量均在脚本组件的监视器中可以找到)。
第三步
为进度条的图标设计一个纹理——我很简单地使用了一个在photoshop里快速制作的1 x20像素的线条。
就是这样了!
视频说明稍后呈上btw...
- //Script by Will Goldstone at Unity3dstudent.com
-
- // set GUI bar width and height in the Inspector
- var barWidth : float = 500;
- var barHeight : float = 25;
-
- // drag a texture as the icon to move on the progress bar
- var progIcon : Texture;
-
- // where to set the GUI element to
- private var barProgress : float;
-
- // empty objects represent the start and end of a level
- var startPoint : Transform;
- var endPoint : Transform;
-
- // current Player position
- var playerPos : Transform;
-
- function Update(){
- // get level distance by subtracting start and end
- var totalDist : float = endPoint.position.x – startPoint.position.x;
-
- // get player distance from start in X axis only so slopes / height doesn't affect result
- var playerDist : float = playerPos.position.x – startPoint.position.x;
-
- //get player's progress as a percentage of the whole distance
- var playerProgress : float = playerDist / totalDist * 100;
-
- //turn the playerProgress percentage back into the scale of barWidth
- barProgress = playerProgress / 100 * barWidth;
-
- }
-
- function OnGUI() {
- // create a GUI group the width of the bar and twice its height
- // in order to leave room for 'Start' and 'End' text under the bar
- GUI.BeginGroup (new Rect (10, 10, barWidth, barHeight*2));
-
- //draw a box as the backing for the progress bar, blank text inside
- GUI.Box(Rect(0,0,barWidth,barHeight),"");
-
- // create a label to draw the progress icon texture, use barProgress var
- // to set its X position, 0 as the Y position and width and height of the texture used
- GUI.Label (Rect (barProgress, 0, progIcon.width, progIcon.height),
- progIcon);
-
- // add start and end labels
- GUI.Label(Rect(progIcon.width/2, 25, 50, barHeight),"Start");
- GUI.Label(Rect(barWidth-30, 25, 100, barHeight),"End");
-
- GUI.EndGroup();
- }