- Home /
Get distance player has run wrong values
I’m having some troubles with my unity game. If my player collides with an object the game over scene is shown, and there I wanna display the distance the player has run.
That's my code
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
transform.Translate(0, 0, speedForward * Time.deltaTime);
run = transform.localPosition.z;
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speedHorizontal;
}
But it doesn’t return the right result (sometimes 298, sometimes 299 ). These values are when I let the player run automatically without moving left or right. When I move left and right the result is always different from the results when i only run forward differently (e.g. it displays as result 294 or 293). Could somebody tell me what’s wrong with my code?
Are you sure transform.localPosition is what you are looking for? Depending on your setup, it could produce results that aren't exactly what you want. I'd have a starting point and use (startingPoint.position.z-transform.position.z).
Your answer

Follow this Question
Related Questions
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
First Person Controller wont stop in web build if w,a,s,d is used 0 Answers
Steering Wheel UI Controller. 2 Answers
how can i use FullScreenMovieControlModeHandheld.PlayFullScreenMovie () ? 0 Answers