Search Unity

Using rigidBody.velocity to move a character

Discussion in 'Physics' started by Benjaben22, Apr 27, 2015.

  1. Benjaben22

    Benjaben22

    Joined:
    Oct 26, 2012
    Posts:
    75
    I'm trying to use this code to move character left/right

    Code (CSharp):
    1. rigidBodyComponent.velocity = (movement * movementSpeed) + new Vector3(0, rigidBodyComponent.velocity.y, rigidBodyComponent.velocity.z);
    but the issue I'm having is that when the character is in the air and moving left into a wall, gravity doesn't force it down. Instead they continue to try to move until input is zero;



    As you can see in the picture, he is kind of just floating there while input isn't zero. s soon as movement equals Vector3.zero, the character would fall. Any insight would be very much appreciated.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,435
    (Random guess)
    Maybe he's sticking in the wall, Try frictionless physics material?
     
  3. Benjaben22

    Benjaben22

    Joined:
    Oct 26, 2012
    Posts:
    75
    That causes the character to slide all over
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you need to constantly be adding gravity to velocity..

    using velocity is a bad way to do it. MovePosition would be better
     
  5. Benjaben22

    Benjaben22

    Joined:
    Oct 26, 2012
    Posts:
    75
    MovePosition will cause your character to teleport through things though however.
     
  6. mubashar437

    mubashar437

    Joined:
    Sep 29, 2014
    Posts:
    28
    have u tried:

    rigidbody2D.AddForce(Vector2);

    velocity is not recommended for movement... by setting velocity constant u are forcing body to have constant velocity thus ignoring the effect of gravity force or any other force and in turn all accelerations.
     
  7. Benjaben22

    Benjaben22

    Joined:
    Oct 26, 2012
    Posts:
    75
    Thanks, what I ended up using was AddForce and using Acceleration forcemode as well as a constant downward vector for gravity.