Two ways to calculate velocity without using Rigidbody, which one is more accurate?

Hi all,

I’ve learn’t two ways to calculate velocity of a 3D game object without using Rigidbody, which are shown below. I attached the two scripts onto a same object (a cube) and I’ve noticed the velocity outcomes of the two methods at the same moment are not exactly the same. Can you please tell me which one can represent the velocity more precisely? And can you please tell me why the two scripts work differently? Thanks a million.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VelocityReader : MonoBehaviour
{
    public Vector3 linearVelocity;
    private float previousRealTime;

    //private GeometryTwist message;
    private Vector3 previousPosition = Vector3.zero;
    private Quaternion previousRotation = Quaternion.identity;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float deltaTime = Time.realtimeSinceStartup - previousRealTime;
        linearVelocity = (transform.position - previousPosition) / deltaTime;
        Vector3 angularVelocity = (transform.rotation.eulerAngles - previousRotation.eulerAngles) / deltaTime;
        Debug.Log("Vlinear" + linearVelocity);
        previousRealTime = Time.realtimeSinceStartup;
        previousPosition = transform.position;
        previousRotation = transform.rotation;
    }
}

And the second script is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjVelocityReader : MonoBehaviour
{
    Vector3 PrevPos;
    Vector3 NewPos;

    Vector3 PrevRot;
    Vector3 NewRot;

    public Vector3 ObjVelocity;
    public Vector3 ObjRotation;
    // Use this for initialization
    void Start()
    {
        PrevPos = transform.position;
        PrevRot = transform.eulerAngles;
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        transform.rotation = Quaternion.identity;

        NewPos = transform.position;  // each frame track the new position
        ObjVelocity = (NewPos - PrevPos) / Time.fixedDeltaTime;
        PrevPos = NewPos;  // update position for next frame calculation
        Debug.Log("ObjVlinear" + ObjVelocity);

        NewRot = transform.eulerAngles;  // each frame track the new rotation
        ObjRotation = (NewRot - PrevRot) / Time.fixedDeltaTime;
        PrevRot = NewRot;  // update position for next frame calculation
    }
}

Ok, a few things are a bit off here. In the first script, you are calculating the delta time like this Time.realtimeSinceStartup - previousRealTime;, but a variable already exists in the Time class that has that ready for you. It’s called deltaTime Time.deltaTime; is all you need, no need to recalculate it. Secondly, the second script is using fixedDeltaTime which is always the same number, whatever you have set in the project settings of unity ( 0.02f is the default I believe ), so you should use Time.deltaTime there as well. Also, you will be getting the readings at different times, because one is calculating in the physics loop (FixedUpdate()) and one is calculating every frame (Update()). But I think the real game changer here is the use of fixedDeltaTime. Take a look at the documentation:



For reading the delta time it is recommended to use Time.deltaTime instead because it automatically returns the right delta time if you are inside a FixedUpdate function or Update function.

Hi highpockets,
Thanks a lot for your reply. Do you think I should use Update() or FixedUpdate() in the two scripts above?
And in the first script previousPosition is declared and assigned Zero(Why do we have to be make it Zero here)below the class definition. However in the second script PrevPos is declared in the start() function but not assigned Zero as shown below. Is there any difference? Thanks a million.
1.

 public class VelocityReader : MonoBehaviour
 {
     public Vector3 linearVelocity;
     private float previousRealTime;
     private Vector3 previousPosition = Vector3.zero;
     private Quaternion previousRotation = Quaternion.identity;
 void Start()
     {
         PrevPos = transform.position;
         PrevRot = transform.eulerAngles;