Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Question about Velocity

Discussion in 'Scripting' started by Jonberia, Jun 14, 2018.

  1. Jonberia


    Dec 2, 2017

    Can someone please explain to me why I could not use rb.velocity twice in my code. When I run my game, the player object cannot jump due to movement using rb.velocity. When I change movement to MovePosition or AddForce, it works fine. I’m just curious to why it wouldn’t work if I used rb.velocity for both Jump and movement.

    Here is my code.
    Code (CSharp):
    1. public float jumpForce;  //set jump's strength.
    2. public float speed;  //how fast the object move.
    4. private Rigidbody rb;  //store rigidbody component
    6. void Start ()
    7.     {
    8.         rb = GetComponent<Rigidbody>();  //Get Rigidbody component  
    9.     }
    11. void FixedUpdate ()
    12.     {
    13.         //Jump
    14.         if (Input.GetButton("Jump") && isGrounded())
    15.         {
    16.             rb.velocity = new Vector3(0, jumpForce, 0);
    17.         }
    19.         //Movement
    20.         float moveHorizontal = Input.GetAxis("Horizontal");
    21.         float moveVertical = Input.GetAxis("Vertical");
    23.         Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
    24.         rb.velocity = movement * speed;
    25.     }
    Thanks in advance.;)
  2. methos5k


    Aug 3, 2015
    The problem is that you're overwriting the velocity with your 'movement' variable. Try changing the 'y' portion of movement so it's "rb.velocity.y".
    Jonberia likes this.
  3. Fab4


    Sep 30, 2012
    Forces apply acceleration on your rb's
    Acceleration changes velocity over time
    To set the velocity to a specific value negates your effects of accelerate or add forces, but also all previous set velocity values. As methos already said you overwrite your velocity result in your script. So the first if statement can never be used in the running game.
    Jonberia likes this.
  4. Jonberia


    Dec 2, 2017
    Thanks! I notice when I did changed y in ‘movement’ variable to rb.velocity.y, it was also being calculated by speed; which did produce some interesting result. Of course, larger numbers send the player object skyward immediately, but smaller numbers gave it nice quick bounces when holding the space key. Once again, thanks you guys for clearing that up for me.