Search Unity

Question Jumping on Slopes using the custom physics class from the unity tutorial

Discussion in '2D' started by dav_ege, Jun 2, 2021.

  1. dav_ege

    dav_ege

    Joined:
    Apr 22, 2019
    Posts:
    17
    Hi! I have been following this tutorial:

    https://learn.unity.com/tutorial/live-session-2d-platformer-character-controller

    I used the physics object class as a base for my platformer controller and it is working very well.
    I ran into a problem regarding slopes though and i have yet to figure out a solution.
    Jumping while on a slope sometimes results in the player jumping really high. I checked the assigned velocity.y value and it is not higher than it would be if the jump height was normal, also it only happens while also moving horizontally, the method i use to let my player character jump works the same way as the one shown in the tutorial series and i am not changing any values regarding gravity around the jump.

    Due to a mix of both a bit of language barrier and a good chunk of slowwittedness i did not completely understand the code when it comes to slopes, i can only assume that for some reason my character is moved upwards due to moving on the slope, and then i apply vertical velocity on top of that, which might explain this behaviour but i just dont know how to test and or fix that.

    Thank your for any suggestions :)
     
  2. mbro514

    mbro514

    Joined:
    Sep 15, 2018
    Posts:
    3
    You are correct about this. Here is a simple fix:

    After the line that says:
    Code (CSharp):
    1. Vector2 moveAlongGround = new Vector2 (groundNormal.y, -groundNormal.x);
    put:
    Code (CSharp):
    1. if (!grounded) moveAlongGround = Vector2.right;
     
    dav_ege likes this.
  3. dav_ege

    dav_ege

    Joined:
    Apr 22, 2019
    Posts:
    17
    That solved my problem, thank you!
     
  4. mbro514

    mbro514

    Joined:
    Sep 15, 2018
    Posts:
    3
    You're welcome! I'm glad I could help. :)
     
    dav_ege likes this.
  5. dav_ege

    dav_ege

    Joined:
    Apr 22, 2019
    Posts:
    17
    Actually, upon further testing i realized that isGrounded is set to false before this line, meaning moveAlongGround will always be set to Vector2.right.
    I am not sure what that means and one problem i am still having is that my character can always walk up any slope even if the slope is too steep. Weirdly enough that problem persists even without the fix you suggested and i am starting to think i should probably learn more about custom physics and try the whole script again. :D
     
    Last edited: Jul 5, 2021
  6. mbro514

    mbro514

    Joined:
    Sep 15, 2018
    Posts:
    3
    Sorry, that's my mistake. I'm used to setting up the code in a slightly different way.

    If you put the
    Code (CSharp):
    1. grounded = false;
    right before the
    Code (CSharp):
    1. Movement (move, false);
    ,
    everything should work fine.
    And also, keep the
    Code (CSharp):
    1. if (!grounded) moveAlongGround = Vector2.right;
    where it was.
    I think everything should work fine.

    Not sure about being able to walk up slopes that are too steep, though. :(