Jittering transform.position values when GameObject is not moving.

Hi Unity Community,

I want to measure a GameObject’s instant speed by computing the distance traveled between two frames and dividing by Time.deltaTime. The GameObject in question is the [CameraRig] from the SteamVR pluggin.

I ran across a very unexpected issue: when I read the transform.position values of my GameObject from the script which is a component attached on it, the x, y and z values keep changing randomly by a small amount (between 1 and 0.01) even when the object is NOT moving AT ALL. If I look up the values displayed in the inspector on the transform itself, the values are steady.

I have tried both putting the script on the GameObject itself and putting it on an empty GameObject and getting the reference of the object I want. In both cases I get noisy position values. Obviously, this is ruining my speed computation, which I want to be exactly 0 when the object is not moving.

Is that a known bug in Unity ? I have reused this script from a previous project and I never had this problem before… What can be causing this? What can I do about it? Below is the code causing the issue:

public Vector3 lastPosition;
public float speed;

void Start ()
{
	lastPosition = transform.position;
}

void FixedUpdate()
{
    Vector3 newPosition = transform.position;
	float distanceTravelled = Vector3.Distance(newPosition, lastPosition); 
	lastPosition = newPosition;
    speed = distanceTravelled / Time.deltaTime;
}

Thanks in advance for your help,

PGCW

OK, I found the reason: my object has a collider that was going through my scene’s ground, hence the jittering, because of collisions with the ground plane… I leave this question and answer in case someone encounters the same issue.