- Home /
How can display an object movement speed in Kmh or Mph ?
Kilometer per hour or/and Mile per hour. I display each camera movement speed. But if i want to convert the speed to kph or mph units ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UserInformation : MonoBehaviour
{
public Text cameraText;
public Text cameraSpeed;
public Text cameraOriginalPosition;
public Transform target;
public SwitchCameras switchcameras;
private Vector3[] originalCamerasPosition;
private Vector3 lastPosition;
private Camera[] Cameras;
private int cameraIndex = 0;
// Use this for initialization
void Start()
{
Cameras = switchcameras.cameras;
if (Cameras.Length > 0)
{
originalCamerasPosition = new Vector3[Cameras.Length];
for (int i = 0; i < Cameras.Length; i++)
{
originalCamerasPosition[i] = Cameras[i].transform.position;
}
}
}
// Update is called once per frame
void Update()
{
if (cameraIndex == Cameras.Length)
cameraIndex = 0;
if (Cameras[cameraIndex].enabled == true)
{
cameraText.text = Cameras[cameraIndex].name;
float camSpeed = (Cameras[cameraIndex].transform.position - lastPosition).magnitude / Time.deltaTime;
cameraSpeed.text = camSpeed.ToString();
lastPosition = Cameras[cameraIndex].transform.position;
cameraOriginalPosition.text = originalCamerasPosition[cameraIndex].ToString();
}
else
{
cameraIndex++;
}
}
}
Answer by · Jun 19, 2017 at 06:34 PM
Since Unity uses meters as default, if you get the distance traveled every second, you will get meters/second. 1 m/s = 3.6 km/h. 1km/h = 0,621371 Mph
Answer by JxWolfe · Jun 19, 2017 at 06:32 PM
transform.x = 5. starting position x = 0 distance traveled 5 meters.
every transform value that is for vector3 is one meter. then math in the time into the equation for the mph/ kmh
best example is a default cube. It is one meter by one meter by one meter, then make another one and move it one over in the x, y, or z axis
A default cube is simply 1x1x1, not 1m x 1m x 1m.
This will be completely dependent on the scaling within his game. We really can't tell what he is scaling to especially if it is a 2D game.
yes, he would have to scale the scene up or down to the m mark
Answer by Orami · Jun 19, 2017 at 06:34 PM
This is completely dependent on the scaling in your game. Due to that fact there is no way to tell you how to do it. If you are using 1 unity unit as 1m you could take velocity/1000 to find kps multiply by 60 for minutes then 60 again for hours and then multiply by 0.621371 to get mph. You gotta figure out what 1m is equal to in game though that is on you not on us. Also how hard is it to google how to convert from kph to mph?
Hey Oriami, "how hard is it to google"? Hey genius not everyone has English and high speed internet to google all day, it's folks like you that ruined Stack with your attitude.
Your answer
Follow this Question
Related Questions
How can i prevent from mouse to collide with thirdpersoncontroller ? 0 Answers
How can i lock the mouse cursor in the middle of the screen ? 1 Answer
Why InputField don't have the property text ? 2 Answers
How can i change the walls height and keep the size for example 100x100 but height 0.1 ? 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer