Quantcast
Channel: Unity3D Student » gui
Viewing all articles
Browse latest Browse all 3

Platformer Progress bar

$
0
0

This is something a few of you have asked me to cover recently so here it is – a Unity Tutorial on how to track player progress during the game.

In order to keep your player’s attention through a platformer you may want to show them some kind of HUD bar with an indication as to their progress, here is a simple example of how to achieve this using GUI commands and a texture. As usual due to site width restrictions the script is also provided here - http://www.pasteit4me.com/2269002

Step one

In this example, we simply use two objects as start and end transforms – these may be empty objects or colliders to stop the player progressing, its up to you. Bear in mind that this example is designed to track progress across a 2D level, so does not take height into account.

Step Two

Write out the script below and attach it to an empty object. Set up your desired bar width and height in the Inspector for the script component. Then drag on the player as the player position transform, and the start and end transform objects to those variables too.

Step Three

Design a texture for the icon of progress – I’ve simply used a 1 x 20 pixel line I made quickly in photoshop.

That’s it!

Video Explanation Coming shortly btw..

//Script by Will Goldstone at Unity3dstudent.com
  1.  
  2. // set GUI bar width and height in the Inspector
  3. var barWidth : float = 500;
  4. var barHeight : float = 25;
  5.  
  6. // drag a texture as the icon to move on the progress bar
  7. var progIcon : Texture;
  8.  
  9. // where to set the GUI element to
  10. private var barProgress : float;
  11.  
  12. // empty objects represent the start and end of a level
  13. var startPoint : Transform;
  14. var endPoint : Transform;
  15.  
  16. // current Player position
  17. var playerPos : Transform;
  18.  
  19. function Update(){
  20.  // get level distance by subtracting start and end
  21.  var totalDist : float = endPoint.position.x – startPoint.position.x;
  22.  
  23.  // get player distance from start in X axis only so slopes / height doesn't affect result
  24.  var playerDist : float = playerPos.position.x – startPoint.position.x;
  25.  
  26.  //get player's progress as a percentage of the whole distance
  27.  var playerProgress : float = playerDist / totalDist * 100;
  28.  
  29.  //turn the playerProgress percentage back into the scale of barWidth
  30.  barProgress = playerProgress / 100 * barWidth;
  31.  
  32. }
  33.  
  34. function OnGUI() {
  35.  // create a GUI group the width of the bar and twice its height
  36.  // in order to leave room for 'Start' and 'End' text under the bar
  37.  GUI.BeginGroup (new Rect (10, 10, barWidth, barHeight*2));
  38.  
  39.   //draw a box as the backing for the progress bar, blank text inside
  40.   GUI.Box(Rect(0,0,barWidth,barHeight),"");
  41.  
  42.   // create a label to draw the progress icon texture, use barProgress var
  43.   // to set its X position, 0 as the Y position and width and height of the texture used
  44.   GUI.Label (Rect (barProgress, 0, progIcon.width, progIcon.height),
  45.         progIcon);
  46.  
  47.   // add start and end labels
  48.   GUI.Label(Rect(progIcon.width/2, 25, 50, barHeight),"Start");
  49.   GUI.Label(Rect(barWidth-30, 25, 100, barHeight),"End");
  50.  
  51.  GUI.EndGroup();
  52. }

Viewing all articles
Browse latest Browse all 3

Trending Articles