Advertisement
Guest User

distance traveled unity script

a guest
Jan 17th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class DistanceTracker : MonoBehaviour {
  7.  
  8.     public float speed;
  9.  
  10.     public Text kmDisplay;
  11.  
  12.     Rigidbody rb;
  13.     Vector3 input;
  14.  
  15.     Vector3 a;
  16.     Vector3 b;
  17.  
  18.     bool takingA = true;
  19.  
  20.     float distance;
  21.  
  22.     void Start () {
  23.         rb = GetComponent<Rigidbody>();
  24.         distance = 0f;
  25.     }
  26.    
  27.     void Update () {
  28.         input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
  29.         rb.AddForce(input * speed * Time.deltaTime);
  30.  
  31.         if (takingA)
  32.         {
  33.             a = transform.position;
  34.             takingA = false;
  35.         }
  36.         else
  37.         {
  38.             b = transform.position;
  39.             takingA = true;
  40.             distance += (a - b).magnitude;
  41.         }
  42.         kmDisplay.text = "Meters traveled: " + distance.ToString();
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement