Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class DistanceTracker : MonoBehaviour {
- public float speed;
- public Text kmDisplay;
- Rigidbody rb;
- Vector3 input;
- Vector3 a;
- Vector3 b;
- bool takingA = true;
- float distance;
- void Start () {
- rb = GetComponent<Rigidbody>();
- distance = 0f;
- }
- void Update () {
- input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
- rb.AddForce(input * speed * Time.deltaTime);
- if (takingA)
- {
- a = transform.position;
- takingA = false;
- }
- else
- {
- b = transform.position;
- takingA = true;
- distance += (a - b).magnitude;
- }
- kmDisplay.text = "Meters traveled: " + distance.ToString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement