Hi I have a moving 2D rigidbody and I want to show the objects velocity in m/s or km/h, but I don’t know how to calculate this or even fake it based on solely the objects rigidbody2d.velocity values.
Does anyone have a formula for converting Unity’s 2D movement into real world or close enough velocity values?
Unity velocity is in units/s. To convert those units, it solely depends on the scale you created your game in. If you let 1 unity unit = 1 meter for all your objects when you create them, then velocity is meters/s.
For 2D, the default pixels per unit is 100, so if your velocity is 1, that would be 100 pixels/s. How many pixels are in a meter depends on the physical display your game ends up on.
2 Likes
I whould just use velocity values fot one game:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class move : MonoBehaviour {
Rigidbody2D rb;
public Vector2 dir;
public float speed;
void Start () {
rb = GetComponent <Rigidbody2D> ();
}
void Update () {
dir = rb.velocity;
speed = dir.magnitude;
//or sqrMagnitude as not so cpu expensive
}
}
and then multiplicate it with float correction.
For 2D, Unity uses Box2D which uses units of MKS (Meters, Kilograms and Seconds). Distance is in meters and Velocity is in meters/sec but as was stated above, you could easily assume it was kilometers/sec or World Units/sec.
A Rigidbody2D speed is the magnutide of Rigidbody2D.velocity which is in m/s.