Difficulty Preserving Momentum While Jumping

Good evening, folks!

I’m designing a 2D platformer in Unity (version 5.3.1f1), but I’m having a lot of trouble preserving my character’s momentum, specifically while airborne. I’ve done some research and tried to implement it in a few different ways based on older search results, but nothing so far has worked.

Essentially, my problem is this: the player can move left and right just fine, and they can even jump around a bit, but whenever they do, they always jump straight up. Any speed they’d built going in either direction is immediately cancelled, and emulating the advice in the thread linked above isn’t helping. It’s still possible to jump sideways by inputting left or right after the jump, but this does not make for fun or precise movement.

I think I may have narrowed down where the issue is coming from via testing, but I’ve no idea how to actually solve it. I was hoping somebody here could help me out with it?

I’ll include my player controller script below, along with my hypothesis on where the problem is and what I’ve tried to do to fix it. I apologize if it ends up kind of longish or I break the forum formatting; it’s actually a lot shorter than it looks. I just tend to over-comment (I’m actually an English student, not a programmer, so I need the baby steps). Sorry if that makes it a pain to read!

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour {

    // Determines player facing, which simplifies animations.
    // Returns true if facing right, else false.
    public bool facingRight = true;

  
    // Returns true if player is able to jump, ie has not done so yet.
    public bool jump = false;

    // The first checks if the player is trying to double jump, the second keeps track
    // of whether they still have a jump to expend.
    public bool doubleJump = false;
    public bool hasdoubleJumped = false;

    // Amount of force exerted on the player when they move.
    // Increase to cause the player to accelerate from idle position faster.
    public float moveForce = 350f;

    // Maximum speed, prevents infinite acceleration.
    public float maxSpeed = .5f;

    // Amount of force applied to player when they jump.
    public float jumpForce = 700;
    public float doubleJumpForce = 500f;

    // I don't know what this is going to do yet, honestly.
    public Transform groundCheck;

    // Returns true if player is on a valid, flat surface from which they can jump normally.
    private bool grounded = false;

    // This is a dirty hack, you might want to come back to this.
    private bool onPlatform = false;

    // This stuff lets the player animate properly and interact with physics objects.
    private Animator anim;
    private Rigidbody2D rb2d;

    // Goldfish collected
    private int count;
    public Text countText;

    // These are all of the sound effects the player character can make.
    public AudioClip GoldfishPickup;
    public AudioClip NormalHop;
    public AudioClip DoubleJump;

 
    void Awake ()
    {

        // Just fetching and storing component references.
        // Nothing to see here, move along.
        anim = GetComponent <Animator>();
        rb2d = GetComponent<Rigidbody2D>();

        count = 0;
        SetCountText();

    }

    void Update()
    {
        // Linecast draws a line down and returns a boolean (true) if it collides with
        // groundCheck, at the player's feet. The latter part of the code ensures it is
        // only checking the ground layer, and not other stuff that you might have going on.
        grounded = Physics2D.Linecast(transform.position, groundCheck.position,
            1 << LayerMask.NameToLayer("Ground"));

        // This is doing the same thing with platforms.
        onPlatform = Physics2D.Linecast(transform.position, groundCheck.position,
            1 << LayerMask.NameToLayer("Platform"));



        // Checks to see if the jump button is pressed, and also if the player is grounded.
        // If you want to add a double jump later, this is where you need to play with the code!
        if (Input.GetButtonDown("Jump") && (grounded || onPlatform))
            jump = true;

        // Allows you to double jump while airborn.
        else if (Input.GetButtonDown("Jump") && (!grounded || !onPlatform))
        {
            doubleJump = true;
        }
      
        // Just animation stuff for jumping.
        else if (Input.GetButtonUp("Jump"))
        {
            anim.ResetTrigger("Jump");
        }

    } 
    // Physics stuff always goes here.
    void FixedUpdate()
    {

        float horizontal = Input.GetAxis("Horizontal");

        // Sets animation speed. Should even get faster as the player accelerates!
        // The Mathf.Abs bit is using the absolute value of the above variable, since
        // we're using Flip() to cheat and reorient our sprites by multiplying the
        // X axis by -1. We always want the animation speed to be positive, right?
            anim.SetFloat("Speed", Mathf.Abs(horizontal));

        // If the amount of force being applied to the player is less than the
        // maximum allowed, we're going to add some more. This works in both directions,
        // since horizontal can be a negative number.
        if (horizontal * rb2d.velocity.x < maxSpeed)
            rb2d.AddForce(Vector2.right * horizontal * moveForce);



        // If we are going too fast somehow, this will slow us down again.
        // Mathf.Sign is doing something fun; it returns a 1 or -1, corresponding to whether
        // the velocity is currently positive or negative (left or right). This way we can
        // clamp the max speed in either direction with one line of code.
        // We're leaving the y axis alone because we don't want to change the player's jump speed.
        if (Mathf.Abs(rb2d.velocity.x) > maxSpeed && !jump)
            rb2d.velocity = new Vector2(Mathf.Sign(rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
      

        // This just makes sure we're animating in the same direction that we're moving.
        // It does this by comparing the positive or negative value for horizontal, with the
        // boolean for which direction we're currently facing. Positive values should mean we're
        // facing to the right, negative to the left.
        // (I actually had to invert these > < signs and I don't know why it works this way.)
        if (horizontal < 0 && !facingRight)
            Flip();
        else if (horizontal > 0 && facingRight)
            Flip();

        // This is making sure you get your double jump back when you land.
        if (grounded)
            hasdoubleJumped = false;

        if(jump)
        {
            // Tells the animator we're jumping now.
            AudioSource.PlayClipAtPoint(NormalHop, transform.position);
            anim.SetTrigger("Jump");
            rb2d.AddForce(new Vector2(rb2d.velocity.x, jumpForce));
            jump = false; // This also needs to be changed in the event of a double jump!
        }

        // In theory, this triggers if the player CAN double jump (has single jumped), and has not
        // done so already.
        else if (doubleJump && !hasdoubleJumped)
        {
            AudioSource.PlayClipAtPoint(DoubleJump, transform.position);

            // This is cancelling out the stacking double jump height. Might want to remove it.
            rb2d.AddForce(new Vector2(rb2d.velocity.x, -1*jumpForce));


            rb2d.AddForce(new Vector2(rb2d.velocity.x, doubleJumpForce));
            doubleJump = false; // This also needs to be changed in the event of a double jump!
            hasdoubleJumped = true;
        }
    }

    // This method flips the orientation of your sprite, by scaling the X axis of the sprite by -1.
    // This reduces the number of animations you need to draw manually. You're welcome, future me.
    void Flip()
    {
        Vector3 spriteScale = transform.localScale;
        spriteScale.x *= -1;
        transform.localScale = spriteScale;
        facingRight = !facingRight;
    }

    //OnTriggerEnter2D is called whenever this object overlaps with a trigger collider.
    void OnTriggerEnter2D(Collider2D other)
    {
        //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
        if (other.gameObject.CompareTag("Pickup"))
        {
            // Plays a sound, deactivates the game object, and then increments total collected.
            AudioSource.PlayClipAtPoint(GoldfishPickup, transform.position);
            other.gameObject.SetActive(false);
            count = count + 1;
            SetCountText();
        }
    
    }

    // Method refreshes total of pickups collected in the UI when called.
    void SetCountText()
    {
        countText.text = "x" + count.ToString();
    }
}

Again, sorry if that was way too much to sift through, but I didn’t want to risk not including some method or another that might actually turn out to be the problem.

I’m fairly certain that the problem is in the bit of code that defines how a player jumps, in line 147. My gut says it’s specifically this bit:

…which, in theory, should be passing both the old value of the player’s velocity on the x axis, with the new force of the jump on the y axis, as arguments. Except, clearly, this isn’t happening. I determined this must be the problem because if I replace rb2d.velocity.x with any static value, positive or negative, the character will hop in that direction! For instance, a value of 30 will cause them to hop to the right; -30 will steer them to the left. This is due to the flip function.

So that’s cool, it tells the line of code is definitely working as intended, in that it is allowing the player to jump while moving on the x axis, but that rb2d.velocity.x is not what I think it is.

A friend of mine took a whack at it, too, and we wrote it a few different ways, but I’ve got no clue what the solution is. Am I just using it incorrectly? Any help would be much appreciated. After all, I can’t imagine a platformer being fun to play if jumping doesn’t even feel good!

Sorry this is such a basic question, and thanks in advance!

Hey again! I don’t mean to pain, but this post spent about three days in limbo since it was my first, and had to be approved. So I’m just bumping it for visibility!

Thanks again to anyone that can offer some insight~

The line you mention (147) is indeed strange. You are setting the numerical value of velocity as a force. Think about it… velocity is measured in m/s while force is measured in N.

However, you are adding a force in the same direction of the velocity and thus it doesn’t explain why the horizontal speed gets canceled. So while line 147 is not correct you have another problem elsewhere.

You can narrow it down quickly by adding some Debug.Log() lines and watch how and when the velocity changes.

Thanks for the response! I actually do not know how Debug.Log() works, can you explain to me how to use it? It sounds like it’d be awfully useful a thing to learn, anyway! Then I’ll get back to you on what’s going on with it.

And what would you recommend putting in place of rb2d.velocity.x in that case, even if it is not the cause of the problem? I’d like to get into the habit of good form.

Here is the doc on Debug.Log(): Unity - Scripting API: Debug.Log
For instance, you could print out the current velocity like: Debug.LogFormat(“My velocity right now is {0}”, rb2d.velocity);

But anyway, assuming you have a rigidbody with default mass of 1, your force of 700 is far too strong for a max speed of 0.5 which is way too low… So what’s happening is your code is applying a force and the object is accelerating way past your max speed. Next frame it won’t apply any force and instead will truncate the speed to 0.5. Then next frame it will apply a force again and so on. This will create a stutter movement. It’s probably also the cause of your horizontal speed disappearing… after you jump, line 125 will truncate the speed to 0.5 which is practically stopping. (Note that the variable ‘jump’ will only be true during one frame, so soon after the jump force is applied, that code will hit again)

I’ve been playing with the debug tool today; thanks again for teaching me how to use it! I knew there had to be a feature like it somewhere and I’m sure I’ll get a ton of mileage out of it!

So, looking at my results, it looks like you’re spot on. That max speed code is kicking in pretty much every frame, because .5f is too low compared to the forces being applied to it. But here’s my dilemma; while changing the maximum speed to something higher does seem to fix the jumping momentum problem (hooray!) it creates a new one because then the character rockets off of the screen every time they move! I’ve tried lowering the force applied when the character moves or jumps to compensate, but that just creates new problems with their acceleration being too low, or not getting off of the ground, respectively.

I think this might be because of the size of the sprites I’m working with. The player is maybe like, 20x20 pixels? So even a little bit of force results in quite a bit of movement. We’re talking this scale:

Are there any values in Unity that I can play with, such as its mass or the force of gravity, that would allow me to implement this fix without firing off into the distance? Or is there something else I’m missing? If I can, I’d like to avoid having to upscale all of the assets (which won’t look as nice, and also necessitate me starting completely over). But in any case, this is actually a lot of progress.

[Edit: Sorry about the half-post, hit enter too quickly.]

A lot of games with tight platforming controls don’t use physics at all. If you implement everything from scratch you have more control.

But it can be achieved with physics too. I certainly use it.

It’s tough to get it just right. It will take a lot of iterations, trial and errors, workarounds…

Some next steps for you:

  • The overall gravity can be found here: Unity - Manual: Physics 2D
  • You can also change the gravity for specific objects individually with the gravity scale: Unity - Scripting API: Rigidbody2D.gravityScale
  • The size of your sprite in pixels doesn’t matter. But the size in meters matter. Try to keep your character around 1 or 2 m height. Scale the world around him accordingly.
  • You can also ‘jump’ by assigning an initial Y velocity. That will make the character jump instantly.
  • Some games let you jump a little higher if you keep holding the jump button. That’s achieved with a mix of initial velocity and an additional force that is applied and decays over time.

Like I said, it’s not easy to get this right and you definitely want to get this right before designing levels. Otherwise later you decide that the character should jump higher and suddenly you have to readjust a ton of levels… been there, done that.

Thanks so much for all the links! I’ll definitely take your advice and start playing with all of these settings until it’s perfect!

Also I forgot to mention but I approve of your username; Grim Fandango is great, and by extension, so are you.
Anyway, thanks again, you’re the best!

1 Like