- Home /
Getting the speed of a gameobject
How can i by using javscript get the speed of a gameobject? I cant use Rigidbody.velocity as the object is not a Rigidbody.
Answer by qJake · May 11, 2010 at 10:10 PM
I just wrote this now, I didn't test it or anything, but you can give it a try if you want. Just drop this into a file called Speed.cs and you can read the public "speed" variable from it to get the speed of the object.
using UnityEngine;
public class Speed : MonoBehaviour { public float speed = 0;
Vector3 lastPosition = Vector3.zero;
void FixedUpdate()
{
speed = (transform.position - lastPosition).magnitude;
lastPosition = transform.position;
}
}
I tryed this, but the output keep fluctuating betwene the actual speed value and zero.
Its better to use a coroutine for this task. Please look at my answer.
It may be a golden shovel award and noob question but how can I replace 2D text with the value from that script??
Well you should've probably created another question, but you'd have to make something like this
using UnityEngine.UI;
public Text screenText;
public float value;
void Start()
{
screenText.text = value.ToString();
}
to get a real speed, you should do speed = (transform.position - lastPosition).magnitude / Time.fixedDeltaTime
Optionally, you can call it 'rawSpeed' and calculate some 'smoothSpeed' in Update() by using SmoothDamp()
Answer by jorjdboss · Feb 29, 2012 at 04:23 PM
Its hardly 8-9 lines of code. Please check out this link to my blog: http://fungamesprogrammer.blogspot.in/2012/02/calculate-velocity-of-non-rigidbodies.html
Answer by JamesL98 · May 26, 2019 at 07:50 AM
Hey all I know this is an old thread but ive been wandering the internet for a few hours trying to find a solution and the closest I got was jorjdboss' response, however it still wasnt working but he was on the right track. Here is the code I modified for any lost puppy wandering the internet like i was (working in 2019.1.2f1). This responds the speed in km/h and rounds it to the nearest whole number and doesn't fluctuate between a value and zero like most of the solutions i was finding. Hope this helps someone.
void Start()
{
StartCoroutine(CalcVelocity());
}
IEnumerator CalcVelocity()
{
while(Application.isPlaying)
{
lastPos = player.transform.position;
yield return new WaitForFixedUpdate();
movesSpeed = Mathf.RoundToInt(Vector3.Distance(player.transform.position,lastPos)/Time.fixedDeltaTime);
}
}
Answer by ankur30884_unclaimed · Jun 07, 2012 at 10:48 AM
calculate speed of object in KM/H
Vector3 lastPosition = Vector3.zero;
void FixedUpdate()
{
dispSpeed = (((transform.position - lastPosition).magnitude) / Time.deltaTime) ;
lastPosition = transform.position;
}
Answer by davedev · May 11, 2010 at 02:34 PM
This might not be the best, official, easiest way, but in an effort to respond first, here's how I've done it one of my projects.
Basically, you get/save the loc from the previous update and then use the difference in distance and time since the previous update to calculate the time:
// determine the amount of time since last update
var curTime = Time.time;
var updateTime = curTime - lastUpdateTime;
// save old loc at beginning of your update loop
var oldLoc = transform.position;
// do something to change the loc on this update
var newLoc = transform.position; var updateDistanceMeters = Vector3.Distance(newLoc, oldLoc);
// my results are in miles per hour var updateDistanceMiles = convertMetersToMiles(updateDistanceMeters);
var updateTimeHours = updateTime/60.0/60.0; calculatedAircraftSpeed = (updateDistanceMiles)/(updateTimeHours);
// save the current time for use on the next update
lastUpdateTime = curTime;
Im having trouble folowing this code, do any one have a suggestion for a more simple solution for this problem?
When you post code, be sure to either surround it with tags, or hilight it and click the Code button in the toolbar.