Hi I want to calculate the Velocity of an object that is moved either by script or animation.
I have put up some simple script but it looks it doesn’t work Because there “Old” position is overriden by new Corutine. Does anybody has a quick fix?
void Update () {
StartCoroutine (MeasureVelocity());
}
IEnumerator MeasureVelocity(){
OldPosition = transform.position;
yield return new WaitForSeconds (1);
PresentPosition = transform.position;
DifrenceInPOsition=PresentPosition-OldPosition;
Debug.Log (DifrenceInPOsition);
}
}
I would do away with that co-routine crap. Here is a much simpler/accurate version.
public Vector3 Velocity;
Vector3 lastPos;
void Start()
{
lastPos = transform.position;
}
void Update()
{
Velocity = transform.position-lastPos;
lastPos = transform.position;
}
IEnumerator MeasureVelocity(){
if (takeOldposition) {
takeOldposition = false;
OldPosition = transform.position;
yield return new WaitForSeconds (0.1f);
PresentPosition = transform.position;
DifrenceInPOsitionDistance = Vector3.Distance (PresentPosition, OldPosition);
Debug.Log (DifrenceInPOsitionDistance);
takeOldposition = true;
VelocityOFobject = DifrenceInPOsitionDistance / Time.deltaTime;
Debug.Log ("Distance "+DifrenceInPOsitionDistance+"speed "+VelocityOFobject);
}
}
}
This works with , but hay your sollution is like 10 times better thx:smile: