I’m creating an animation controller that I want to use on lots of different game objects. All of which would have different controllers (physics, navmeshagent, user input, etc). I want to use the transform.localposition in order to get forward velocity relative to the object (ie. local). What I have works great except when an object moves directly in the x-axis in either direction. At that point it shows a velocity of nearly 0 (even if moving quickly). I’ve gone through all the forums and tried basically all the different options but nothing is working. What am I doing wrong?
See below for my latest code attempts all of which show similar results.
option #1:
Vector3 previousPosition;
float forwardVelocity(){
Vector3 curMove = transform.localPosition - previousPosition;
float curSpeed = curMove.magnitude / Time.deltaTime;
previousPosition = transform.localPosition;
return curSpeed;
}
option #2:
if(Time.deltaTime > 0){
currentPosZ = transform.localPosition.z;
historyPosZ = posHist;
locVelocity = (transform.localPosition.z - posHist)/Time.deltaTime;
posHist = transform.localPosition.z;
return locVelocity;
}
else{
return 0.0f;
}
option #3… uses rigidbody and still doesn’t work. I would rather not use rigid body though as not all objects in my game need it.
currentVelocity = transform.InverseTransformDirection(rigidbody.velocity).z;