Hello! I’m working on a racing game with a long distance explore/campain mode. I have a speedometer with a working needle, but I think it will add a touch of realism to have a GUIText odometer underneath it. I have a GUIText object created, but no code to go with it.
My car does have a rigidbody attached. I’m not sure if I can use rigidbody.position or a similar method to display the mileage in a GUIText form.
I’m sorry that I don’t have any sample code, being relatively new to racing games! Thank you in advance for any answers/comments/suggestions you may have!
Edit: I’m sorry for referring to the odometer as “kilometer” before and for any confusion it may have caused!
Well, is the car traveling in a straight line? If so, you just need to calculate the distance between your starting location and wherever you’re at, now.
private var startPosition : Vector3;
function Start() {
startPosition = transform.position;
}
function Update() {
float distance = Vector3.Distance(startPosition, transform.position);
}
On the other hand, what if your car’s path is twisting and turning constantly? Well, in that case you could estimate the distance it travels during each frame.
private var totalDistance : float;
private var lastPosition : Vector3;
function Start() {
lastPosition = transform.position;
}
function Update() {
float frameDistance = Vector3.Distance(lastPosition, transform.position);
totalDistance += frameDistance;
lastPosition = transform.position;
}