Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question CharacterController.velocity returns 0 before calling CharacterController.Move()

Discussion in 'Scripting' started by iacco, Nov 19, 2022.

  1. iacco

    iacco

    Joined:
    May 6, 2013
    Posts:
    4
    Hello,

    I was trying to understand how the Third Person Controller from the Starter Assets works by rewriting it to make it work on my custom scene, however I stumbled upon an issue with the CharacterController class.
    The field CharacterController.velocity alwasys returns a value of (0,0,0) when queried before CharacterController.Move(). If I query it after i call Move() then it works just fine.
    I've stripped all other lines of code just to be sure, so the resulting script is simply

    Code (CSharp):
    1. void Update()
    2. {
    3.     Debug.Log(characterController.velocity);
    4.     characterController.Move(transform.forward * Time.deltaTime);
    5.     Debug.Log(characterController.velocity);
    6. }
    The documentation for CharacterController.velocity states that

    The velocity returned is simply the difference in distance for the current timestep before and after a call to CharacterController.Move or CharacterController.SimpleMove.

    Does this mean that .velocity returning 0 before calling .Move() is the intended behaviour? The Third Person Controller made by Unity uses the velocity field before calling the Move method and it returns the correct value but it doesn't seem to be working for me.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Yes. If you haven't moved since the last call to
    .Move()
    , the the delta in position is zero, thus, velocity is zero.
     
  3. iacco

    iacco

    Joined:
    May 6, 2013
    Posts:
    4
    Then how comes that in the Unity Third Person Controller script the .velocity field returns a non-zero value even though it is called before the .Move() method?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,606
    Probably because something it is moving it before
    .velocity
    call? Your behaviour matches what the documentation states; just work with it.