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.

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;
    }
}

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);
        }
        
    }

Its hardly 8-9 lines of code. Please check out this link to my blog:

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;

Vector3 lastPosition = Vector3.zero;

void FixedUpdate()
{
    dispSpeed = (((transform.position - lastPosition).magnitude) / Time.deltaTime) ;
    lastPosition = transform.position;
}

calculate speed of object in KM/H

Vector3 lastPosition = Vector3.zero;

void FixedUpdate()
{
    dispSpeed = (((transform.position - lastPosition).magnitude) / Time.deltaTime) ;
    lastPosition = transform.position;
}

because i think this topic is still hot, here my solution:

you can just save the whole code as TransformSpeed.cs and put it on the transform object.

using UnityEngine;
using System.Collections;

public class TransformSpeed : MonoBehaviour
{
    //Define the Variables
    Vector3 lastPosition = Vector3.zero;
    float Speed = 0;

    void FixedUpdate()
    {
        // To get the Speed in km/h
        //
        // first calculate the distance of the Transform Object between the fixedupdate calls with '(transform.position - lastPosition).magnitude'
        // Now you know the 'meter per fixedupdate'
        //
        // divide this value by Time.deltaTime to get meter per second
        //
        // to get meter per minute just use 'm/s * 60'
        // meter per hour then is 'm/s * 60 * 60'
        // finaly calculate the meter into kilometer by dividing it by 1000 -> 'm/s * 60 * 60 / 1000'
        //
        // just to make the calculation a bit smarter type '1 * 60 * 60 / 1000' in your calculator and you get 3,6
        // you can use this value instead of the long calculation to multiply it with the original m/s:

        Speed = (transform.position - lastPosition).magnitude / Time.deltaTime * 3.6f;
        
        //don't forget to update your lastposition after the calculation
        lastPosition = transform.position;
    }

    void OnGUI()
    {
        // a simple way to display the speed in km/h of the Transform Object
        GUI.Label(new Rect(10, 10, 100, 50), Speed.ToString("##0 km/h"));

        // the string expression in ToString("##0 km/h") just formats the string as you whish
        // a quick overview of the options here: http://alexonasp.net/samples/stringformatting/
    }
}