I am pretty new in Unity,
In creating a running game, i required to add in the meter to show how far the player had run.
I am totally no idea in creating it.
Some hints and helps is needed from you.
Thanks for the help.
Do you need help with keeping track of how far the player has run? Or with displaying this information on screen?
ya.i need to keep tracking and update the meter that the player run, and the information would show it on screen as well.
thanks for replying my post and hopefully can get some help from you =)
For displaying the distance, the most immediately straightforward solution would probably be to use GUI/GUILayout.Label(). See the GUI scripting guide for more info.
As for tracking the distance, how is your character represented? Character controller? Rigid body? How are you moving the character?
displaying the meter already be done. thanks.
for the tracking distance, first person control is used for the character
the player would move forward in a track with the control of left,right and jump.
Ok, a couple more questions:
-
Do you want to measure the total distance moved by the player, even if (e.g.) the player runs back and forth relative to the track or backwards along it? Or are you only concerned with the distance traveled along the track? (Assuming there is a track of some sort.)
-
If there is a track, is it strictly linear? Or does it curve?
the player will only keep moving forward with a constant speed, therefore, there are no backward function in this game.
as the player collide with certain obstacles in the game, this game will be end, and thus the distances traveled by the player will be displayed on the screen.
and yes, the track is linear w/o curve.
thanks for the patience from u.=)
Hehe, no problem ![]()
I think we almost have an answer to your question here. My next question is, how is the track oriented with respect to world space? Is it axis aligned? (That is, is it parallel to the x, y, or z world axis?) If so, with which axis is it aligned, and is the ‘forward’ direction along the positive or negative axis?
ya. the axis is aligned towards the z positive axis.
=)
It that case, it seems the distance you’re looking for would simply be the difference between the player’s initial z position and the player’s current z position. (At any given time, the value of transform.position.z will give you the player’s position with respect to the z axis.)
ok…i will try figure it out. thanks for the help. =)
if i got problem, hope u can help me again.hehe
is there any function for me to track the current position of the game object in unity?
sorry for trouble again.
Javascript
var player : Transform;
Debug.Log(player.transform.position.x);
Pseudo, but I think that will work…
ya. it works just fine.
Thanks =)
I actually gave that same example (that is, transform.x/y/z) in my earlier post above
But, glad you got it working.
Try this. Be warned, I have never written a single line in JS (I use C#). This should note your position and set run distance when the game begin. On update it will add the difference between transform.position and lastPosition, after that it will set lastPosition to the position you have now. This might give errors as I typed it right here in the browser, but you’ll get the idea.
CODE REMOVED: See fixed version two posts down
That won’t work; it’ll just give you a vector representing the player’s summed displacement from the starting position.
If you accumulated the distance rather than the summed displacement vector itself, you’d have the total distance traveled. However, depending on the nature of the game, this might not be entirely suitable either. (For example, the player could accumulate distance by running backwards or side to side with respect to the track.)
^My bad, I stand corrected. Here, now it should work properly.
var runDistance : float;
var deltaMovement : Vector3;
var lastPosition : Vector3;
function Start()
{
runDistance = Vector3.zero;
lastPosition = transform.position;
}
function Update()
{
deltaMovement += transform.position - lastPosition;
runDistance += deltaMovement.magnitude;
lastPosition = transform.position;
}
function OnGUI()
{
GUI.Label(Rect(5, 5, 200, 30), "Distance ran: "+runDistance);
}
‘vector2 cannot convert to float’ i get, any idea? i make 2d game