Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to ignore friction when setting velocity?

Discussion in 'Physics' started by protaxX, Oct 13, 2023.

  1. protaxX

    protaxX

    Joined:
    Sep 23, 2023
    Posts:
    3
    HI, for some reason i want to make player movement from two parts. The first is the physics part. It's needed to make inertia and acceleration on slopes, for example. And the second one is for responsive movement, so I want to control velocity directly for this part. For this movement I made simple function witch was supposed work. I control only second part, the rigid body with low friction material should take care of the rest.

    Code (CSharp):
    1. private void Move()
    2. {
    3.     _inputVelocity = _playerInput.Player.Move.ReadValue<Vector2>();
    4.     _inputVelocity = new Vector3(_inputVelocity.x, 0f, _inputVelocity.y);
    5.  
    6.     if (IsGrounded())
    7.     {
    8.         _inputVelocity *= movementFactor;
    9.     }
    10.     else
    11.     {
    12.         _inputVelocity *= airMovementFactor;
    13.     }
    14.     _inputVelocity = transform.TransformDirection(_inputVelocity);
    15.     Debug.Log(_inputVelocity);
    16.     _rb.velocity -= _previousInputVelocity;
    17.     _rb.velocity += _inputVelocity;
    18.  
    19.     _previousInputVelocity = _inputVelocity;
    20. }
    But this doesn't work both in Update and FixedUpdate. It only works in case of zero friction. But I need it for proper slope slide. How do I make this right?
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    757
    Remove line 16 because it could make your character go backwards instead of just stopping. It could also prevent your character from falling when in the air.

    I suspect you're just struggling getting the right balance between force and friction. Let the physics engine bring your character to a stop. You can either increase material friction or adjust the rigidbody drag. You could also set a higher drag value when on the ground by setting _rb.drag.
     
  3. protaxX

    protaxX

    Joined:
    Sep 23, 2023
    Posts:
    3
    But how do I make responsive movement part? I think I can represent my movement with two velocity vectors. First for physics and second for responsive movement. Second velocity vector is _inputVelocity. So I just replace old _inputVelocity every Update. Falling in the air works just fine for some reason.

    I think it's because the friction applies after setting velocity in script, so I can't set velocity ignoring friction.
     
    Last edited: Oct 14, 2023
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    757
    Instead of messing with _rb.velocity try this:

    Code (CSharp):
    1. _rb.AddForce(_inputVelocity,ForceMode.VelocityChange);
     
  5. protaxX

    protaxX

    Joined:
    Sep 23, 2023
    Posts:
    3
    After a few experiments, I think I've reached desired behavior. This line of code solves the problem:

    Code (CSharp):
    1. _rb.MovePosition(_rb.position + _inputVelocity * Time.deltaTime);
    In this way I can move character without interfering into rigid body velocities. Thank you for help!