Search Unity

Rigidbody.velocity

Discussion in 'Scripting' started by Konniskatt, Jul 10, 2018.

  1. Konniskatt

    Konniskatt

    Joined:
    Jun 24, 2018
    Posts:
    26
    Hi, I have this script:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class Speedometer : MonoBehaviour{
    5.     public Rigidbody Rgbd;
    6.     void Start(){
    7.         Rgbd = GetComponent<Rigidbody>();
    8.     }
    9.  
    10.     void FixedUpdate() {
    11.         print(Rgbd.velocity);
    12.     }
    13.  
    14. }
    You are supposed to write on the console the speed in meters per second, or rather units per second.
    But no, write me this: (1.2, 0.0, 41.7)
    I'm getting angry because no matter how hard I try to make it work, the Unity documentation is useless. How is something as simple as a speedometer so difficult to program?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Could you elaborate on what's wrong? That value is the units per second.
     
  3. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Are you looking for maybe the magnitude of the velocity?
     
  4. Konniskatt

    Konniskatt

    Joined:
    Jun 24, 2018
    Posts:
    26
    is for a car game anyways thanks i will see if i can make a fake speedometer
     
  5. Konniskatt

    Konniskatt

    Joined:
    Jun 24, 2018
    Posts:
    26
    ISN'T! Sometimes throw random number, negative numbers, ugh. Thanks. Apparently, making a speedometer is a stupid endless mistery. It's harder than finding a needle in a haystack....
     
  6. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Velocity encodes direction and speed. The value you are getting is the units-per-second separated in the X, Y and Z axis.

    If you want a single number to use for a speedometer, use the magnitude of that vector:

    Code (CSharp):
    1. print(Rgbd.velocity.magnitude);
     
    hippocoder likes this.
  7. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Regardless, the problem isn't anything to do with Unity. It's the fact you don't understand vectors. I'm only mentioning this because it's likely it will keep happening til you realise these are general problems that aren't anything to do with the engine (velocity is a Vector3 after all, and those have been around in C# and other languages forever - it's just 3 floats).
     
    bobisgod234 likes this.
  8. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    speed is basically a "simple" math function. speed = distance / time.

    where things can get tricky is that there are numerous units of measure, and you have to account for them when making something like a speedometer.

    for instance, unity units are in meters, and the deltaTime per frame is in milliseconds. you have to convert those values into the units you need in order to make a speedometer.

    Code (CSharp):
    1. public class MilesPerHour : MonoBehaviour
    2. {
    3.     public float milesPerHour = 0f;
    4.  
    5.     [Space(20)]
    6.  
    7.     public float metersTravelledPerSecond;
    8.     public float metersTravelledPerMinute;
    9.     public float metersTravelledPerHour;
    10.  
    11.     // Got this value from a google search. Unity uses meters,
    12.     // and one meter = 0.000621371f miles.
    13.     private float metersToMiles = 0.000621371f;
    14.     private Vector3 prevPosition = Vector3.zero;
    15.  
    16.     // Use this for initialization
    17.     void Start ()
    18.     {
    19.         prevPosition = transform.position;  
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update ()
    24.     {
    25.         Vector3 v = transform.position - prevPosition;
    26.  
    27.         float distance = v.magnitude;
    28.         if (distance < 0.0001f)
    29.             distance = 0f;
    30.  
    31.         // Get the meters travelled per second.
    32.         metersTravelledPerSecond = distance / Time.deltaTime;
    33.  
    34.         // Meters per minute. There are 60 seconds in one minute!
    35.         metersTravelledPerMinute = metersTravelledPerSecond * 60f;
    36.  
    37.         // Meters per hour. There are 60 minutes in one hour!
    38.         metersTravelledPerHour = metersTravelledPerMinute * 60f;
    39.  
    40.         // Finally, multiply are speed - which is in meters - by our conversion variable to get the miles per hour.
    41.         milesPerHour = metersTravelledPerHour * metersToMiles;
    42.  
    43.         prevPosition = transform.position;
    44.     }
    45. }
     
  9. Konniskatt

    Konniskatt

    Joined:
    Jun 24, 2018
    Posts:
    26
    Thanks, I already solve my problem. BTW, I use metrical system
     
  10. Konniskatt

    Konniskatt

    Joined:
    Jun 24, 2018
    Posts:
    26
    Ok, finally worked, but the numbers are in float. I need in integer. I tried this:
    Code (CSharp):
    1. RigidVel = Rgbd.velocity.magnitude * 3.6 //converts meters per hour to kilometers
    2. Debug.Log(RigidVel.ToString("0"));
    But what happen? Unity is now dumb as me and says: "15,8: Unexpected symbol "Debug""
    wtf
     
  11. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You forgot a semicolon on the end of line one.
     
    hippocoder likes this.
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    (and an f)
     
  13. Konniskatt

    Konniskatt

    Joined:
    Jun 24, 2018
    Posts:
    26
    oh, idiot am I. I am using vs code, because, uh, the genius developers of unity removed monodevelop, and now we have that trash of vs, which is very slow.