Search Unity

Rigidbody.velocity and transform.forward

Discussion in 'Physics' started by Rubikpotato, Apr 2, 2015.

  1. Rubikpotato

    Rubikpotato

    Joined:
    Dec 25, 2014
    Posts:
    14
    Hi, this is the current code I'm using to move the player:
    Code (CSharp):
    1. float Horizontal = Input.GetAxis("Horizontal");
    2.      float Vertical = Input.GetAxis("Vertical");
    3.    
    4.      if(Input.GetKey("w"))
    5.      {
    6.         rigidbody.velocity = transform.forward * (Vertical * speed);
    7.      }
    8.      if(Input.GetKey("s"))
    9.      {
    10.         rigidbody.velocity = transform.forward * (Vertical * speed);
    11.      }
    12.      if(Input.GetKey("a"))
    13.      {
    14.         rigidbody.velocity = transform.right * (Horizontal * speed);
    15.      }
    16.      if(Input.GetKey("d"))
    17.      {
    18.         rigidbody.velocity = transform.right * (Horizontal * speed);
    19.      }
    The problem here is that when the character is falling and I press any move key it falls slower (like gliding) and also doesn't let the player jump until you release the key. I use rigidbody.velocity to move the player because it's smooth and constant rather than rigidbody.addforce slugish movement. I think it can be resolved with rigidbody.addforce, but I can't figure how to move the player with addforce and make the movement smooth (as rigidbody.velocity) and with constant speed.
    Anybody know how I can fix this?
     
  2. waythewanderer

    waythewanderer

    Joined:
    Apr 6, 2015
    Posts:
    92
    When the character is falling, gravity is already acting upon it. And you modify the velocity through the keys; physics is being messed with.

    Try Rigidbody.MovePosition instead. Remember to use this in FixedUpdate ;)
     
  3. Rubikpotato

    Rubikpotato

    Joined:
    Dec 25, 2014
    Posts:
    14
    Yay! thank you, it works! but I noticed that the movement have no sliding at all (like rigidbody.velocity and the float Vertical), so I tried with a physic material but it did not work, how I can achieve a sort of sliding?
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,283
    So, you were getting sliding because the character slows down naturally as you weren't setting the velocity anymore. When you do MovePosition, the character doesn't get any velocity, and won't slide.

    You can implement sliding manually - if you're not getting any movement input, instead of not calling movePosition, you lerp from the last input towards Vector3.zero, and use that for movement.

    Or, you could use the vertical and horizontal axes to set the x- and z-components of your velocity, and copy the old y-velocity:

    Code (CSharp):
    1. float Horizontal = Input.GetAxis("Horizontal");
    2. float Vertical = Input.GetAxis("Vertical");
    3.  
    4. //Check should cause sliding.
    5. if(Horizontal != 0 || Vertical != 0) {
    6.     Vector3 movement = new Vector3(Horizontal, 0, Vertical) * speed;
    7.     rigidbody.velocity = new Vector3(movment.x, rigidbody.velocity.y, movement.z);
    8. }
     
  5. Rubikpotato

    Rubikpotato

    Joined:
    Dec 25, 2014
    Posts:
    14
    Yeah but with this I'm not longer moving the player based on its rotation, I will be moving the player in fixed coordinates
    Edit: I just fixed it with a CharacterController, my god that thing was always over my face and I didn't see it.
     
    Last edited: Apr 8, 2015
  6. Gaming_geko

    Gaming_geko

    Joined:
    Oct 17, 2017
    Posts:
    2
    My amazing solution:

    ` yVel = rb2d.velocity.y;
    rb2d.velocity = new Vector2(Input.GetAxis("Horizontal"), yVel);`

    The problem is, it is getting the velocity (for me Y) and setting it, every frame, however the script is getting the velocity (for me y so i'm going to be saying it's y) let's say 1, so it sets it to 1, great right? wrong, gravity is constant, however the object is going from static to moving, so gravity accelerates the object, however the code `rigidbody.velocity = new Vector2 (10f, rigidbody.velocity.y);` will keep the y velocity constant, not allowing for the previously mentioned acceleration. to fix this, I made a variable, specifically for the Y velocity, but it gets set before the velocity does (it's in the Update method), so the acceleration can happen.

    I had a major problem with this and it was a Eureka moment xD