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();
-
}