Platformer Progress bar
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..
-
-
// 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();
-
}










[...] This post was mentioned on Twitter by Will Goldstone and david hovanky, Paul Stapelberg. Paul Stapelberg said: RT @willgoldstone New Unity3Dstudent.com example post- Making a Player Progress Bar for a platformer http://bit.ly/hSSRwq #unity3d [...]
I get several errors from this script…
Can’t check right now but haven’t heard issues from anyone else – what are the issues? Always mention the errors!
I get no errors from this script..
I got this error:
(19,46): BCE0044: unexpected char: 0xFFFD.
in the script error:
var totalDist : float = endPoint.position.x – startPoint.position.x;
Just find the problem. forget the previos post;)
I’ve got the same issue. Use “-” instead of “–”
But then I receive much more errors. Basically:
BeginGroup, Box, Label, EndGroup is not a member of GUI…
Any hints?
Probably a copy paste issue, write it manually and see if it helps! good luck Michal
How did you cameara follow for character!?
There is an example follow script in the Standard Assets. Go to Assets > Import and look at custom packages. Import the ‘Scripts’ package.
Hey, where do I find the script for getting the character moving? I don’t know enough about scripting to write one yet.
then you should work up to it! don’t run before you can walk. When you’re ready theres an example on one the character controllers package you can import from the assets menu.