Search Unity

Rigidbody velocity close to 0 when moving

Discussion in 'Physics' started by gregor039, Sep 1, 2019.

  1. gregor039

    gregor039

    Joined:
    Mar 9, 2019
    Posts:
    4
    Hello,

    I am quite new to Unity and am at a loss with the velocity of Rigidbodies.

    In my scene, I have a terrain, cube with a box collider (default settings), a Rigidbody (default settings), and the following script.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class test : MonoBehaviour
    5. {
    6.  
    7.     void Update()
    8.     {
    9.         float horizontalMovement = Input.GetAxis("Horizontal");
    10.         float verticalMovement = Input.GetAxis("Vertical");
    11.  
    12.         float movementMultiplyer = 2f;
    13.  
    14.         if (verticalMovement != 0)
    15.         {
    16.             transform.Translate(Vector3.forward * verticalMovement * Time.deltaTime * movementMultiplyer);
    17.         }
    18.  
    19.         if (horizontalMovement != 0)
    20.         {
    21.             transform.Translate(Vector3.right * horizontalMovement * Time.deltaTime * movementMultiplyer);
    22.         }
    23.        
    24.         Debug.Log(transform.GetComponent<Rigidbody>().velocity.z);
    25.  
    26.     }
    27. }
    28.  
    When moving the cube I want to read its forward velocity.
    Somehow the cube moves at around 2 units/s, but the Debug output prints values in the range of E-07 to E-05, so basically 0.
    As the Unity Documentation stats:
    Did I understand smth. wrong/where did I make a mistake/how to get the actual speed along an axis?
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    transform.Translate just "teleports" the object, so that's not perceived as velocity. You should either move the rigidbody using the Rigidbody.AddForce method, or mark it as Kinematic and use Rigidbody.MovePosition and Rigidbody.MoveRotation. In both cases you must call those methods from FixedUpdate, not from Update.

    EDIT: Welcome to Unity! :)
     
  3. gregor039

    gregor039

    Joined:
    Mar 9, 2019
    Posts:
    4
    Thank you!

    The Documentation threw me off with
    . :D

    The AddForce function (all ForceModes) only worked, when the "Use Gravity" option was disabled on the cube. Do you know where that could come from?

    For the script, I changed the Update to FixedUpdate, removed the Translate and added the AddForce

    Code (CSharp):
    1.     void FixedUpdate ()
    2.     {
    3.         float horizontalMovement = Input.GetAxis("Horizontal");
    4.         float verticalMovement = Input.GetAxis("Vertical");
    5.  
    6.         float movementMultiplyer = 200f;
    7.  
    8.         if (verticalMovement != 0)
    9.         {
    10.             GetComponent<Rigidbody>().AddForce(Vector3.forward * verticalMovement * Time.deltaTime * movementMultiplyer, ForceMode.Acceleration);
    11.         }
    12.  
    13.         if (horizontalMovement != 0)
    14.         {
    15.             GetComponent<Rigidbody>().AddForce(Vector3.right * horizontalMovement * Time.deltaTime * movementMultiplyer, ForceMode.Acceleration);
    16.         }
    17.        
    18.         Debug.Log(transform.GetComponent<Rigidbody>().velocity.magnitude);
    19.  
    20.     }
     
  4. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    This could come from the friction that your body was having with the collider it was standing on.
    Also when dealing with forces I suggest at least PI controller (Proportional Integral)
     
    gregor039 likes this.
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    You have to remove Time.deltaTime from the calculation too. The acceleration is a time-independent magnitude. It gets integrated along the timestep by the physics solver. Then adjust your magnitudes or multipliers accordingly.

    And yes, the cube doesn't move because of the friction with the surface when gravity is enabled. You may want to learn about physics materials and frictions now ;)

    https://docs.unity3d.com/Manual/class-PhysicMaterial.html
     
    gregor039 likes this.
  6. gregor039

    gregor039

    Joined:
    Mar 9, 2019
    Posts:
    4
    Indeed the friction was the problem.

    The cube is moving by now, but I still have some questions around this topic:
    - Should only the cube, or the terrain, or both have PhysicMaterial?
    - Is this (Collider, Rigidbody, PhysicMaterial) the "best" way to handle humanoid movement? The outcome of this way feels more vehicle-related.

    Do you maybe have a good resource on hand about setting up a movement system? The ones I found usually use the provided CharacterController, transform.translate, or NavMeshAgents.