Mph help

Discussion in 'Scripting' started by Tyler Ridings, Feb 27, 2011.

  1. Tyler Ridings

    Tyler Ridings

    New Member

    Joined:
    Aug 28, 2009
    Messages:
    201
    Ok guys here is my problem.I am using this script to calculate my mph speed

    Code (csharp):
    1. var myObject : GameObject;
    2. var gui : GameObject;
    3.  
    4. function Update () {
    5.        
    6. gui.guiText.text = "Speed: " + myObject.rigidbody.velocity.magnitude.ToString() + "m/h";
    7.  
    8. }
    But when i get my vehicle up to around 80 it freaks out and behaves weird so im just gonna make an illusion of doing 80.I need to know how instead of calculating miles per hour i can do some smaller then miles like feet maybe.What im wanting to do is when your doing 40 mph the speedo and script displays you doing 80.

    That way i can keep my vehicle behaving how i need but trick the player to thinking they are doing 80.
  2. Antitheory

    Antitheory

    New Member

    Joined:
    Nov 14, 2010
    Messages:
    549
    Just have the speedometer display myObject.rigidbody.velocity.magnitude*2 or something.
    Also you should note that rigidbody.velocity is not in miles/hour. It is in units/second. By "units" I mean Unity's units. This could be meters, miles, feet, whatever... It depends on how large your models are.

    Some games (notably Grand Theft Auto) uses techniques to make it look like you are going faster when you really aren't. Increasing the FOV (field of view of the camera) can make it look like you are moving faster. In GTA they also make the other cars move slower so it feels like you are moving faster.
  3. bigkahuna

    bigkahuna

    New Member

    Joined:
    Apr 30, 2006
    Messages:
    5,419
    @Tyler - You are aware that "rigidbody.velocity.magnitude" does not result in speed in miles per hour, right? I believe the result is in meters per second by default, which you'll need to convert if you want mph.
  4. Tyler Ridings

    Tyler Ridings

    New Member

    Joined:
    Aug 28, 2009
    Messages:
    201
    No i had no idea.I was told or well read in another thread that it was already in miles per hour.Ooops.How would i go about converting it?
  5. bigkahuna

    bigkahuna

    New Member

    Joined:
    Apr 30, 2006
    Messages:
    5,419
    I could be wrong, but the way I understand it the default for all physics is in metric units: meters, kilograms, and m/sec. It should be easy enough to build a test to confirm it. If I get a chance tomorrow I might try to do just that.
  6. pakfront

    pakfront

    Member

    Joined:
    Oct 6, 2010
    Messages:
    496
  7. Tyler Ridings

    Tyler Ridings

    New Member

    Joined:
    Aug 28, 2009
    Messages:
    201
    Sounds good bigk!So i see that right now im already where i want to be by what pak says.Because my truck behaves fine at 40 but not 80 but sence this is in meters that means when im doing 40 meters im doing 80 miles!So its pretty much already where i need it but see what you can come up with bigk!
  8. Antitheory

    Antitheory

    New Member

    Joined:
    Nov 14, 2010
    Messages:
    549
    It's not in metric, the units are completely undefined. AFAIK the only metric constant in Unity is Physics.gravity which is in m/s^2... but of course you can change gravity to whatever you like.

    I personally define units as meters, so when I make/import a model I can scale it to that size. It makes physics calculations a lot easier as most physical constants and relationships are metric.
  9. bigkahuna

    bigkahuna

    New Member

    Joined:
    Apr 30, 2006
    Messages:
    5,419
    That's what we've been told, but one would have to believe that it's not entirely true. Assuming the physic's gravity standard is 9.8 m/s**2, then how could you use, for example, "feet" for a distance standard and still have accurate physics simulation? Mathematically it's not possible. Although UT has said that you can use any measurement standard you want, they have also said that in order to maintain physics accuracy you should use meters as your distance standard. Thus my assumption that rigidbody.velocity.magnitude is measured in meters per second. I'm assuming this because I haven't read anything (from UT or PhysX) to the contrary.
  10. Antitheory

    Antitheory

    New Member

    Joined:
    Nov 14, 2010
    Messages:
    549
    Well I completely agree that using meters as your unit is the way to go. I debated this very point in another thread where others were arguing the opposite (that you could choose any arbitrary measure, like the length of your hand or something). In that thread I had to concede that using meters in a game of microscopic or planetary scale wouldn't make much sense, especially because gravity and other simple physics equations give way to more complex relationships at such scales.

    And in response to your question about having accurate physics simulation if you use feet as your units. It's still possible, you just have to change the gravity constant to feet per second. Physics.gravity = 9.8*Vector3.down*feetPerMeter;
  11. andeeeee

    andeeeee

    Unity Technologies

    Joined:
    Jul 19, 2005
    Messages:
    8,729
    The way to understand this is to note that the mathematical relationships (ie, the formulae) between force, acceleration, mass and velocity are the same regardless of the units used. For example, the relationship
    Code (csharp):
    1. Force = Mass x Acceleration
    ...is valid regardless of the units of mass and acceleration you use and indeed the units of force will be determined by them. So, if you use pennyweights for mass and furlongs per fortnight squared for acceleration then the units of force will be pennyweight furlongs per fortnight squared. (Amazingly, Google can actually convert pennyweight furlongs per fortnight squared into newtons for you if you ask nicely ;-)

    The units of mass and distance in Unity are arbitrary, so you can't say what speed you are "really" going at. However, by default, the time unit corresponds directly to seconds of real time and the gravitational acceleration is 9.8. Since Earth's gravity is nominally 9.8 m/s^2, this means that Unity's default gravity corresponds to Earth gravity if you regard Unity's distance units as metres.

    There isn't any datum constant in Unity to set the value of mass so the units of mass and force are still arbitrary. However, it is probably easiest to maintain consistency if you think of the mass as kilograms and the forces as newtons.
  12. bigkahuna

    bigkahuna

    New Member

    Joined:
    Apr 30, 2006
    Messages:
    5,419
    Ok, now I'm completely confused. :p

    If gravity in Unity is set to 9.8 (which corresponds to 9.8 m/s**2, a known constant) then doesn't rigidbody.velocity.magnitude show speed/velocity in m/sec? If it doesn't, then of what use is it and how do we find velocity?
  13. Antitheory

    Antitheory

    New Member

    Joined:
    Nov 14, 2010
    Messages:
    549
    If your models correspond to 1 unit = 1 meter, than indeed rigidbody.velocity is "in" meters per second....
    Keep in mind that Physics.gravity is not really in meters/second^2, it represents units/second^2. It just happens that Earth's gravitational pull is 9.8m/s^2, so if you want to use feet or furlongs everywhere you'll need to convert this constant into your units.
    Last edited: Mar 1, 2011
  14. bigkahuna

    bigkahuna

    New Member

    Joined:
    Apr 30, 2006
    Messages:
    5,419
    @Antitheory - Ok, we're on the same page then. So for all intents and purposes, for the average user who doesn't edit the gravity settings, they should assume rigidbody.velocity is in m/sec which was my original answer.
  15. PrimeDerektive

    PrimeDerektive

    Member

    Joined:
    Dec 13, 2009
    Messages:
    2,959
    Assuming everything in your game is scaled to be 1 unit = 1 meter. You could bring a 1/100th scale 3d model of the united states into Unity, and bring a 1:1 scale model of a dodge viper into unity and drive it around. Is it going 10000mph, or is it going 100mph in a smaller united states? Everything is relative to the scale of the objects in the game.
  16. Tyler Ridings

    Tyler Ridings

    New Member

    Joined:
    Aug 28, 2009
    Messages:
    201
    Ok so size matters! So how can we calculate velocity no matter the size of the vehicle like real world. Or should we just do our best to keep everything on a certain scale and then just work from their with what unity already provides.
  17. pakfront

    pakfront

    Member

    Joined:
    Oct 6, 2010
    Messages:
    496
    if you are doing 'realistic' stuff, like a racing game or your average FPS, the easiest thing to do is go with 1 unit = 1 meter. This is the default assumption that unity physics make. So, if you want to build an average sized human, make him 1.7 units tall in Unity, equivalent to 1.7 meters tall in real life. Then velocity is in meter per second

    If you are doing 'realistic' stuff in space or at a microscopic level, then consider thinking of the world as 1 unit = 1 mm, 1 nm, or 1 km, as appropriate. You'll want to adjust default physics gravity, and possibly some other stuff.

    If you are making a non-realistic game, such as Little Big Planet, I'd think of your your scale so that your characters are 1 (or maybe 2) units tall. Then you can think of speed as 'character heights per second'.

    IMHO avoid english measure (pounds, slugs, feet) as you will soon go batty. And I'm saying that as an someone who likes miles and inches, just not for math.
    //edit when i say avoid englush measure, I mean for your world scale and game math. It's no problem to display MPH in the gui by simply multiplying your game meters/sec number by the right scaling factor.
    Last edited: Mar 2, 2011