I've been trying to give my character some drag when it turns in the opposite direction, but for some reason my acceleration variable only seems to apply itself when my horizontal velocity is 0. For example, if my character is standing still, and my horizontal acceleration is set to 0.5 seconds, it takes my character 0.5s to reach its maximum velocity. However, if I reach this velocity, and then begin to turn around, the transition is much faster. This doesn’t make sense to me because it should take less time to go from 0 to 10 than it does to go from 10 to -10.

It is very easy to see this difference in my game, as even if I change my acceleration to 3s, once I reach my maximum velocity I can turn around almost instantly. I'm using the smoothdamp function, and have tried a few other methods than the one in the code below, one of which distinguished between two distinct velocity smoothing variables. I felt that this created a better feeling of drag, but could result in jerky movement depending on input.

Should I be using smoothdamp for this kind of movement, or is there a more effective solution? Ideally I'd like to have this drag increase or decrease according to my current velocity.

private void FixedUpdate () 
{
    HandleInput();
}

private void SetHorizontalForce(float x)
{
    if (directionalInput.x != 0)
    {
        velocity.x = Mathf.SmoothDamp(velocity.x, x, ref velocityXSmoothing, 
                (State.IsCollidingBelow) ? Parameters.accelerationTimeGrounded : Parameters.accelerationTimeAirborne);
    }

    else if (directionalInput.x == 0) //Prevents the character from 'sliding' if suddenly moving left and right at low velocities
    {
            velocity.x = Mathf.SmoothDamp(velocity.x, 0, ref velocityXSmoothing, Parameters.stopTime);
    }
} 

private void HandleInput()
{
    directionalInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

    if (!Controller2D.State.IsClimbingLadder && !Controller2D.State.ClampLadderMovement)
    {
        SetHorizontalForce(directionalInput.x * stats.movementSpeed);
    }
  • 1
    (1) What you call "drag" is actually inertia. Drag is something else (akin to atmospheric dampening - e.g. a parachute enables you to fall slowly, because it has a high drag coefficient). (2) my horizontal acceleration is set to 0.5 seconds, it takes my character 0.5s to reach its maximum velocity This inverts the meaning of acceleration. In physics, the higher your acceleration is, the less time you need to reach a given top speed. But in your code, setting the acceleration to a higher value makes the object accelerate slower, which is counterintuitive. – Flater May 29 at 11:28
  • 1
    Have you considered to use a rigidbody and cause movement by adding forces? It might give you a more physically plausible gameplay. – Philipp Oct 6 at 13:03

Your SmoothDamp function can take a fifth maxSpeed parameter. See if giving it a proper limit works.

 if (directionalInput.x != 0)
    {
        velocity.x = Mathf.SmoothDamp(velocity.x, x, ref velocityXSmoothing, 
                (State.IsCollidingBelow) ? Parameters.accelerationTimeGrounded : Parameters.accelerationTimeAirborne),
                 maxTransitionSpeed;
    }
  • This does seem to add drag, but could you explain why? I don't understand why limiting the max speed would cause this, especially when the limit exceeds my input. My maximum possible velocity is 8 and -8, but if I set maxTransitionSpeed to ~14 I get my desired drag. – Ginger and Lavender Feb 24 at 19:18
  • I'd say it's because smoothDamp has a fixed time for going from one value to another, if for example, smoothDamp would always reach its target value in 1 second then transition speed would be 8 units per second for 0 to 8 and twice that for -8 to 8. Putting a max limit on the transitionSpeed would prevent the smoothDamp from adapting its speed. I am mostly guessing this, you can look into this link instead for better info. devblog.aliasinggames.com/smoothdamp – Aeiou Feb 25 at 5:15

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.