Velocity values jumping around...

I’m calculating velocity for a transform (without a rigidbody) and I’m noticing the magnitude values jump around between small and large values when the transform is moving at constant speed. Does anyone know how to fix it?

using UnityEngine;
using System.Collections;

public class VelocityCalculator : MonoBehaviour
{
    Vector3 previousPosition;
    Vector3 velocity;
    float magnitude;

    void Start ()
    {
        previousPosition = transform.position;
    }
 
    void Update ()
    {
        velocity = (transform.position - previousPosition) / Time.deltaTime;   
        previousPosition = transform.position;
        magnitude = velocity.magnitude;
                
        Debug.DrawRay(transform.position,velocity,Color.red);
        print ("magnitude: " + magnitude);      
    }
}

Your code looks ok to me…maybe it depends on how you change the position. If you do it like in FixedUpdate this might cause your behaviour,

I suppose that scripts order is a problem. VelocityCalculator is sometimes invoked before move and sometimes after move. I suggest to change Update method into LateUpdate or change order (Edit->ProjectSettings->ScriptsExecutionOrder)