Ramps going down Ramps going Up, ramps ramps ramps

Hello there!

So in my platformer, i’ve been following along with this course, supplementing it with other knowledge too, and I ran into a problem. Once the feet boxcollider is in, everything works fine. apart from i was getting stuck on ramp corners.

I managed to fix that though by making the Feet collider a trigger, so i can now run UP ramps. However, when trying to run DOWN ramps the character gets stuck in a painfully slow decent. you can still move and jump but the speed goes to superslow.

Anyone got any ideas as to how to fix this please? I have included my code below :slight_smile: and a picture of the colliders.

private enum MOVESTATE {idle, running, jumping, falling, climbing}

Vector2 moveInput;

[SerializeField] private float jumpHeight = 14f;
[SerializeField] private float runSpeed = 7f;
[SerializeField] private float climbSpeed = 5f;
[SerializeField] private LayerMask jumpableGround;

private Rigidbody2D playerBox;
private CapsuleCollider2D myBody;
private BoxCollider2D myFeet;
private SpriteRenderer sprite;
private Animator playerAnim;

private Vector2 x_playerMovement, y_playerMovement;

private float dirx = 0f;
private bool isClimbing = false;        
private float gravityAtStart;


private void Awake()
{
    playerBox = GetComponent<Rigidbody2D>();
    myBody = GetComponent<CapsuleCollider2D>();
    myFeet = GetComponent<BoxCollider2D>();
    sprite = GetComponent<SpriteRenderer>();
}
void Start()
{
    //playerBox = GetComponent<Rigidbody2D>();
    //coll = GetComponent<Collider2D>();
    //sprite = GetComponent<SpriteRenderer>();
    playerAnim = GetComponent<Animator>();
    gravityAtStart = playerBox.gravityScale;
}


void Update()
{
    //playerBox.velocity = new Vector2(dirx * runSpeed, playerBox.velocity.y);        
    Run();
    ClimbLadder();
    UpdateAnim();
}

void OnMove(InputValue value)
{
    moveInput = value.Get<Vector2>();
    Debug.Log(moveInput);
}

void OnJump(InputValue value)
{
    //if(!coll.IsTouchingLayers(LayerMask.GetMask("Ground"))) { return; }


    if (value.isPressed && IsGrounded())
    {
        //vector 2 as we are 2dimensional 
        playerBox.velocity += new Vector2(playerBox.velocity.x, jumpHeight);

    }
}

void ClimbLadder()
{
    if (!myFeet.IsTouchingLayers(LayerMask.GetMask("Ladder"))) 
    {
        playerBox.gravityScale = gravityAtStart;
        isClimbing = false;
        return; 
    }

    y_playerMovement = new Vector2(playerBox.velocity.x, moveInput.y * climbSpeed);
    playerBox.velocity = y_playerMovement;
    playerBox.gravityScale = 0f;
    isClimbing = true;
}
void Run()
{
    x_playerMovement = new Vector2(moveInput.x * runSpeed, playerBox.velocity.y);
    playerBox.velocity = x_playerMovement; 
}

/// <summary>
/// Here we set up the Sprite animation direction and usage. dependent on user iunput.
/// The enum is called to check the state of the player wether they are running jumping falling or standing still
/// </summary>
private void UpdateAnim()
{
    MOVESTATE state;

    if (playerBox.velocity.x > 0f)
    {
        state = MOVESTATE.running;
        sprite.flipX = false;
    }
    else if (playerBox.velocity.x < 0f)
    {
        state = MOVESTATE.running;
        sprite.flipX = true;
    }
    else
    {
        state = MOVESTATE.idle;
    }

    //the below block has higher priority than running so that we don't get "running in the air"
    if (playerBox.velocity.y > .1f && !IsGrounded())
    {
        state = MOVESTATE.jumping;
    }
    else if (playerBox.velocity.y < -.1f && !IsGrounded())
    {
        state = MOVESTATE.falling;
    }

    if (isClimbing)
    {
        state = MOVESTATE.climbing;
    }
    //sets the state to the correct number so that the animator controller can pick up the change. ENUM's can be converted to int values using the (int)ENUM argument. so that each enum value gets converted to a digit, starting at 0. (like an array)
    playerAnim.SetInteger("moveState", (int)state);
}private bool IsGrounded()
{
    return (myFeet.IsTouchingLayers(LayerMask.GetMask("Ground")));
   //return Physics2D.BoxCast(myBody.bounds.center, myBody.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
} 

image

1 Like

Hi Kheldarath,

It’s nice to see that you are challenging yourself. :slight_smile:

What are ramps in the context of your game? Please share more information about them, and maybe also screenshots of the problem. Since the colliders are involved, seeing them in the screenshot(s) will certainly be helpful.

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

in the picture attached you sould be able to see the platforms and the ramps.
As said, going up the ramps is fixed, thats all going great now. its just the coming down the big ramp on the right. Im not sure how to get the player to “stick” to the ramp incline. You should be able to see the two colliders in the previously posted picture of the player, there is a capsule collider and a box collidertrigger.

I think I need a gravity check or a raycast check of somekind, but i cant seem to find the right combination :slight_smile: Any hints or help would be welcomed thank you!

Thank you for the screenshot. What I’m also interested in is a screenshot of the problem/situation when the player is walking down the ramp. Does he rotate or does “walk” with the corner of the feet/box collider? Or does he walk with the capsule collider?

Have you already tried to set the Collision Detection of the player’s Rigidbody2D component to “Continuous”? If not, try that because if your problem is caused by the physics simulation, “Continuous” could solve it.

Theoretically, if moving the ramp up, moving the ramp down should also work if the player does not change its structure.

Check the player’s velocity with a Debug.Log to figure out if he is stuck due to the velocity or due to something else.

Hi there.

The player, she doesn’t walk at all she just sort of falls slowly. Im using a capsule collider for the players body, is it possible that its due to the shape? I mean,

\ ( )
\ (_)
\

(diagram of contact) I gues the Capsule collider doesn’t properly intersect with the Angle of the flat slope when going down. And the feet collider therefore remains not in contact with the ground. I could make the foot collider longer, but then I’d run into the issue of sticking when jumping up against the flat walls perhaps?

Yes, that’s what I suspect. Test the foot collider anyway. It does not matter if you stick to the wall because that’s a problem Rick solves in one of the videos (“Prevent Wall Jumps”). Or didn’t his solution work for you?

thanks Nina. I will let you know how it turns out!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms