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

Get speed from a rigidbody with 360 movement

Discussion in 'Physics' started by gabrimo, May 17, 2020.

  1. gabrimo

    gabrimo

    Joined:
    May 2, 2015
    Posts:
    92
    Hi all, I have a 360 movement system that moves a kinematic rigidbody at seemingly constant speed on screen. This is my current (resumed) movement code:

    Code (CSharp):
    1. movementVector= new Vector3(inputDirection.y, 0, -inputDirection.x) * jogSpeed * Time.deltaTime;
    2. rigidbody.MovePosition(transform.position + movementVector);
    I would like to get the speed value of that rigidbody, but my results aren't good so far. Already did some attempts with this codes on Update():

    Code (CSharp):
    1. //RESULTS FROM ~10 TO ~23
    2.         /*currentPosition = transform.position - previousPosition;
    3.         distance = currentPosition.magnitude;
    4.         if (distance < 0.0001f)
    5.             distance = 0;
    6.         footballerSpeed = (distance / Time.deltaTime) * 3.6f;
    7.         previousPosition = transform.position;*/
    8.  
    9.         //RESULTSFROM 0.08 TO 0.15
    10.         //footballerSpeed = GetComponent<Rigidbody>().velocity.magnitude * 3.6f * Time.deltaTime;
    11.  
    12.         //RESULTS FROM 0.35 TO 0.50
    13.         //footballerSpeed = movementByInputController.movementVector.magnitude * 3.6f;
    14.  
    15.         //RESULTS FROM 400 TO 950
    16.         //footballerSpeed = GetComponent<Rigidbody>().velocity.magnitude / Time.deltaTime;
    17.  
    18.         //RESULTS FROM 8 TO 19
    19.         //footballerSpeed = movementByInputController.movementVector.magnitude / Time.deltaTime;
    As you can see, no matter the formula, values are presenting too much variation for a player moving at constant speed, I would like to get a constant KM/H value here. Another doubt is about the formulas themselves, already did some googling before come here, but none of the formulas purposed worked for me. Maybe it's something basic, I don't know, I'm practically on trial and error here. Any help would be appreciated.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,497
    Ensure that your motion code (movementVector, MovePosition etc) is called from FixedUpdate. Then you may read Rigidbody.velocity.magnitude anywhere and it will contain the actual velocity of the body in m/s.
     
    gabrimo likes this.
  3. gabrimo

    gabrimo

    Joined:
    May 2, 2015
    Posts:
    92
    Many thanks sir, it worked. I know that codes regarding physics must be placed on FixedUpdate, but I have a friend of mine who recommended put the movement code on Update(), cause in this case, it was for a kinematic rigidbody and this would improve the movement flow. I forgot that I had this code on the wrong place cause everything was working good so far.