2
\$\begingroup\$

I originally posted this on Stack Overflow, but was told I might have more luck here:

I've been slowing grinding out programming features for my character controller, and one of the last things to implement is ladder climbing. The code I've come up with over the past 2 days mostly works, but presents some issues:

  1. Sometimes the character bounces after climbing while holding the up arrow, at the point with which a 1-way platform meets the end of the ladder.
  2. Once the character is all the way up, resting on the 1-way platform, it cannot go back down. This can be remedied with a longer ladder collider, but that makes issue #1 worse.
  3. If the character is on the ground near a ladder and jumps, but presses the up arrow at the very last second, it gets flung considerably far in a certain direction.
  4. If the character is on a rope and jumps off the rope, but holds the up arrow before doing so, it goes slightly higher, but is noticeable. Not holding the up arrow, but rather just pressing the left or right arrow and jumping, yields a regular jump. Perhaps this has something to do with the x or y input axes?

The way my classes work is that I have a Controller2D class which handles most of the raycasting stuff and movement methods, a Player class for input that calls the movement methods, and a parameters class that sets default world parameters, like gravity and jump height, but can be overridden if the character is in a certain volume, such as water or a ladder collider (this allows me to set gravity to 0 while climbing so the character can move properly). Almost all of my ladder code is contained in the OnTriggerEnter/Stay/Exit functions, and I'm just not sure if this is the best way to do it.

I tried making a ClimbUp/ClimbDown method, and shooting a raycast from the center of the player, which would allow him or her to climb up, but this was not overly successful (there were too many issues compared to using OnTriggerStay).

I'm wondering if someone who has successfully implemented ladder climbing in a raycast controller, or anybody who might have advice, would be willing to point me in the right direction for this task.

Here's some of the script:

public void ClimbLadder(float y)
{
    ClimbingOnLadder = true;
    SetVerticalForce(y);
}

public void JumpWhileClimbing(float x)
{
    if (ClimbingOnLadder)
    {
        ClimbingOnLadder = false;
        overrideParameters = null;
        AddForce(new Vector3(x, Parameters.jumpVelocity * .70f, 0));
        JumpAfterClimbing = true;
        print(Parameters.jumpVelocity);
    }
}

private void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Ladder")
    {
        CanClimb = true;
    }
}

private void OnTriggerStay2D(Collider2D other)
{
    if (other.tag == "Ladder")
    {
        if (ClimbingOnLadder)
        {
            var newParameters = other.gameObject.GetComponent<ControllerPhysicsVolume2D>();

            if (newParameters == null)
            {
                return;
            }

            overrideParameters = newParameters.NewParameters;
            Debug.Log("overriding");

            if (ClimbingOnLadder && State.IsCollidingBelow)
            {
                ClimbingOnLadder = false;
                overrideParameters = null;
            }
        }
    }
}

private void OnTriggerExit2D(Collider2D other)
{
    if (other.tag == "Ladder")
    {
        CanClimb = false;
        ClimbingOnLadder = false;
        JumpAfterClimbing = false;
    }

    var newParameters = other.gameObject.GetComponent<ControllerPhysicsVolume2D>();

    if (newParameters == null)
    {
        return;
    }

    overrideParameters = null;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.