Search Unity

[SOLVED] Calculating Velocity is Inconsistent

Discussion in 'Scripting' started by Deleted User, Jun 11, 2018.

  1. Deleted User

    Deleted User

    Guest

    Hello,

    I'm in the process of syncing animations over a network and have run into a strange issue. I have opted for an approach where each client calculates the velocity of each player and then play animations accordingly. If the player is moving forward at x speed then play the walk animations etc. I am unable to use rigidbody.velocity or charactercontroller.velocity, as all players positions are just extrapolated over the network (Meaning the move functions are not used in either of those scripts and as a results the velocities are always zero). Consequently, I need to create my own method for calculating the players velocities, and thus, I get to the issue I am having.

    Below is a simple script that I created which calculates a transforms speed. (The issue I am having affects both speed, and velocity. For simplicity, I am using speed here)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class VelocityCalculator : MonoBehaviour {
    6.  
    7.     [SerializeField] private float m_Speed;
    8.     private Vector3 _lastPos;
    9.     private Vector3 _currentPos;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         _lastPos = transform.position;
    14.         _currentPos = transform.position;
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void LateUpdate () {
    19.         var distance = (_lastPos - _currentPos).magnitude;
    20.         m_Speed = distance / Time.deltaTime;
    21.  
    22.         _lastPos = _currentPos;
    23.         _currentPos = transform.position;
    24.     }
    25. }
    26.  
    Now, I would expect this to update m_Speed to the current speed of what every this is attached to, and for the most part it does. However, watching the speed variable in the inspector, I noticed that every now and them the speed will drop to 0 for a few frames, even if my player is currently moving in the scene. This causes my players to stutter between different animations as it adjusts to the current speed.

    My question is simply, why am I seeing this artifact and is there a way to calculate the current speed of the player without it happening?
     
  2. Deleted User

    Deleted User

    Guest

    I believe that I have found the issue to the problem. I was under the impression that LateUpdates were called after unity handles movement. As a result, it made sense to perform the calculations there. However, after adding a few debug messages, I could see that _lastPos and _currentPos where the same meaning that LateUpdate was not working in the way I thought it was. So I read up on the difference between Update, FixedUpdate, and LateUpdate. It turns out, that these calculations should be done within the FixedUpdate even though that is where the movement logic is handled. This has fixed the issue, and the speed/velocities are giving constant numbers.