Tracking Velocity Without Rigid Body HELP

hey all I’m trying to track the velocity of an object that doesn’t have a rigid body i found a script online in C# and it worked perfectly until it just stopped working for some reason. i didn’t edit the script at all i didn’t change the scripts location it just doesn’t work

here the script

using UnityEngine;
using System.Collections;

public class HandVelocity : MonoBehaviour {

	public Vector3 currVel;
	Vector3 prevPos;


void  Update ()
{
  // Position at frame start
  prevPos = transform.position;
  StartCoroutine( CalcVelocity(prevPos) );
}
 
IEnumerator  CalcVelocity( Vector3 pos )
{
  // Wait till it the end of the frame
  // Velocity = DeltaPosition / DeltaTime
  yield return new WaitForEndOfFrame();
  currVel = (pos - transform.position) / Time.deltaTime;
  Debug.Log( currVel );
}
}

normally the a vector 3 would display in the debug log but now it just says 0,0,0 when my object is moving
VERY CONFUSED

Thanks for looking AND thanks a lot if you can help.

PS sorry if i posted this in the wrong place i don’t use the forums much

Some things to check: presumably the .currVel field is being set in the Unity editor. Use the inspector to look at the object with the HandVelocity script attached to it in order to make sure it didn’t become zero, because that is what seems might have happened based on your Debug output.

If you don’t set .currVel in the editor, i.e you set it by another script somewhere else, check that code to make sure it is being called.

You can also hard-code a starting value (such as Vector3.one) into .currVel and see if the object starts moving.

Finally, you can simplify the HandVelocity routine a bit like so:

using UnityEngine;
using System.Collections;

public class HandVelocity : MonoBehaviour
{
    public Vector3 currVel;

	void Update()
	{
		transform.position += currVel * Time.deltaTime;
	}
}

Best,
Kurt

You can get velocity without the coroutine:

void Update(){
if (prevPos != null){
currVel =( prevPos - transform.position)/Time.deltaTime;
Debug.Log(currVel);
}
prevPos = transform.position;
}