Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How can i make a responsive RigidBody FP movement without setting constant rb's velocity?

Discussion in 'Scripting' started by solidp26, Nov 13, 2022.

  1. solidp26

    solidp26

    Joined:
    Nov 1, 2018
    Posts:
    20
    This is my movement code right now:

    Code (CSharp):
    1. Vector3 inputMoveV3 = new Vector3(inputMove.x, 0, inputMove.y);    
    2.         moveVector = transform.TransformDirection(inputMoveV3) * movementSpeed * movementSpeedMultiplier;
    3.         playerDir = Vector3.SmoothDamp(playerDir, moveVector, ref playerVelocity, movementSmoothing);
    4.         rb.velocity = new Vector3(playerDir.x, rb.velocity.y, playerDir.z);
    Problem with this method is that i cant add any additional velocity to the rigidbody. For example player being pushed back from an explosion.

    Any help?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,893
    You can add directional vectors together to get their combined effects. So, simply put, just combine all the movement influences at the end.

    Generally I do all the movement direction calculations in
    Update()
    and store that in a field local to the class. Then, in this case as you're using Rigidbodies, just use that field in
    FixedUpdate()
    to apply movement to the rigidbody.

    That said, if you're simply setting the velocity of a rigidbody for your character you're probably better off using the CharacterController component.
     
  3. solidp26

    solidp26

    Joined:
    Nov 1, 2018
    Posts:
    20
    Give this man a true.

    Don't know how i didn't figure this out myself, thx.