Search Unity

Rigidbody.velocity is receiving value, but it does not move.

Discussion in 'Physics' started by Shadownildo, Jan 26, 2019.

Thread Status:
Not open for further replies.
  1. Shadownildo

    Shadownildo

    Joined:
    May 31, 2018
    Posts:
    10
    Hello, I'm making a dodge system for the player, and the Inputs are working perfectly, what happens is that I do not know if I'm correctly applying the RigidBody.Velocity.
    The player already moves based on RigidBody.Velocity, it constantly receives from Horizontal and Vertical Axis. Is this really the right approach? Or is there some other way? (AddForce does not bring the desired effect)

    I'm not going to show the entire code, but is almost the same for all directions,

    Code (CSharp):
    1.  private void InputDetector_OnInput(PlayerMovement.InputData data)
    2.     {
    3.         Debug.Log("The Input is : " + data.Direction);
    4.         if (data.Direction == PlayerMovement.InputDirection.Up)
    5.         {
    6.  
    7.             DashVerify(1);
    8.         }
    9. }
    10.     private void DashVerify(int direction)
    11.     {
    12.         if (Input.GetKeyDown(KeyCode.Space))
    13.         {
    14.             if (dashTime <= 0)
    15.             {
    16.  
    17.                 direction = 0;
    18.                 dashTime = startDashTime;
    19.                 rb.velocity = Vector3.zero;
    20.                 Debug.Log("Clear All");
    21.  
    22.             }
    23.             else
    24.             {
    25.                 Debug.Log("DashVerify Else Condition");
    26.                 dashTime -= Time.deltaTime;
    27.                 Dash(direction);
    28.             }
    29.         }
    30.     }
    31.  
    32.  
    33. public void Dash(int direction)
    34.     {
    35.    
    36.         switch (direction)
    37.         {
    38.             case 1:
    39.                 //Cima
    40.  
    41.                 rb.velocity = Vector3.forward * dashSpeed;
    42.  
    43.                 Debug.Log("Forward Dash  : " + rb.velocity);
    44.                 break;
    45. }
    46. }

    ===========================================================================

    In this example, the Debug.Log, Receive (0,0, 0,0, 100,0) what is correct, but not Move...
     
    Last edited: Jan 26, 2019
  2. code.PNG
    Please edit your post and use code tags for your code.
     
    Shadownildo likes this.
  3. Shadownildo

    Shadownildo

    Joined:
    May 31, 2018
    Posts:
    10
    Sorry about that, i didn't know how do it. Thanks!
     
    Lurking-Ninja likes this.
  4. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    It's better to use Rigidbody.AddForce with the option "ForceMode.VelocityChange" instead of modifying the velocity parameter directly. You may calculate the velocity change to apply as "velocityChange = targetVelocity - rigidbody.velocity".
     
    Shadownildo likes this.
  5. Shadownildo

    Shadownildo

    Joined:
    May 31, 2018
    Posts:
    10
    You talk just referring to the Dash system or any type of movement. and if it is ... why AddForce? I have already used it and it has an artificial effect. Does he have any kind of benefit?
     
  6. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Shadownildo likes this.
  7. AndrueAndersonCS

    AndrueAndersonCS

    Joined:
    Mar 20, 2014
    Posts:
    4
    I ran into this problem as well, setting velocity but nothing was happening. Turns out if your rigidbody is set to isKinematic then it does not apply velocity.
     
    Risky_Crash, pjbaron and IndyTrogdor like this.
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    Please check the dates of posts before necroing them. This is from 2019.
     
Thread Status:
Not open for further replies.