Trouble with jumping script

I’m making a top down character controller and I’m struggling to get the jumping to work right.

Basically I want the jump to keep the initial movement and slowly slow down, but while it slows it also increases air control.

And it actually works perfect when keeping the initial speed AND slowly increasing air control.

The only problem is that one seems to cancel out the other.

SO if I cut out the section that tells is to keep initial speed, air control gradually kicks in.

AND if I cut out the air control, the initial jump maintains the direction and slows over time.

BUT if I do both at the same time the character launches straight up in the air and slowly gains air control.

IT’S maddening…

Anyway, here’s the function:

    void Movement(){
        //set inputs
        xOffset = Input.GetAxisRaw("Horizontal");
        zOffset = Input.GetAxisRaw("Vertical");

        //Turn to facing direction
        facingDirection = new Vector3(transform.position.x + xOffset, transform.position.y, transform.position.z + zOffset);
        transform.LookAt(facingDirection);

        //Walking
        if (isGrounded == true){
            //Apply Movement
            movementDirection.Set(xOffset, 0f, zOffset);
            movementDirection = movementDirection.normalized * movementSpeed * Time.fixedDeltaTime;
            myRigidbody.MovePosition (transform.position + movementDirection);
        }
       
        //Jumping
        if (jumping == true) {
            //Get control values
            jumpControl += Time.fixedDeltaTime;
            if (jumpControl > jumpControlDelay){
                jumpControl = jumpControlDelay;
            }

            //Add current movement
            myRigidbody.MovePosition(transform.position + (movementDirection * (1 - (jumpControl / jumpControlDelay))));

            //Add air control movement
            airMovementDirection.Set(xOffset, 0f, zOffset);
            airMovementDirection = airMovementDirection.normalized * airSpeed * Time.fixedDeltaTime;
            myRigidbody.MovePosition(transform.position + airMovementDirection * (jumpControl / jumpControlDelay));
        }

Thanks for giving it a look!

What you are doing seems reasonable. The only change I would do is to is combine the current movement and the air movement and apply it as a single .MovePosition() to the rigidbody, just to cut down on computations.

One thing that sticks out to me is that you are checking the isGrounded boolean, then you are checking the jumping boolean. Is the jumping boolean going to be true all the time you want it to be, or only when you are holding the jump button? Would it be more appropriate to use the !.isGrounded condition? Just some thoughts.

To figure this out, I would put various of the values (bools, speeds, counters, timers, etc.) onscreen in real time, using a UnityUI text object in the corner of the screen, to try and see what is happening in real time as you jump.

1 Like

That’s a good suggestion. I just didn’t want to make another vector3.

As far as the bools, yeah I definitely need to just make a state machine.

Thanks.

Technically, you HAVE made a state machine! :slight_smile:

But it can be useful though to drive bools from a series of discrete states that you define in advance, and then make sure that there are only valid combinations of those bools driven from the acceptable valid state table.