- Home /
non-rigidbody velocity wrong in x-axis
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;
In option 1 you would appear to be better off using position rather than local position I think. That is the objects change in world position. It should clearly be called forwardSpeed though - that's turning it into a scalar - is that what you want?
using position ins$$anonymous$$d of localPosition would alter all the axis right? So that the forward velocity would constantly change in relation to the direction the object is moving in the world space. I want to make sure that the velocity I receive is in relation to the object. It shouldn't matter which direction the object is moving as long as they are moving in the local z-axis there should be a positive z velocity.
Well localPosition is just the position of an object within it's parent - so it would change the axis only if the object is moving relative to its parent and the parent is moving relative to the world - is that what is happening?
no, it is not an nested object. So does this mean that I should use position but then at some point use the inverseTransformDirection function to get a vector in relation to the object as opposed to the world.
$$anonymous$$aybe we are talking at cross purposes here - but isn't the difference in position of the object actually relative to the object? I must be missing what you want...
Answer by tobobox · Jun 22, 2012 at 01:06 AM
whydoidoit: See the picture below to demonstrate my point. Both world and local velocity describe the velocity of the object but the local velocity rotates it's coordinates in relation to the direction that the object is facing. So you can see in the example image that the same velocity described both ways ends up with two different values. The world velocity has a negative z value where the local velocity has a positive z value.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073112im_/https://answers.unity.com/themes/thub/images/avi.jpg)