Movement

Hey all,

I currently have an issue with my platformer movement.
Currently my player can walk left and right and jump, however when we introduce a slope, the player can easily walk up and when stopping mid-way, the player will slowly glide down (as seen in this gif)

What I want to achieve is that the steeper the slope gets, the slower the player moves when moving upwards.
And when the angle of the slope is like 45+ degrees. the player is no longer able to move up the slope and will slowly glide downwards until it stops on ground that is between 45 and -45 degrees rotation. Next to that, if the slope is walkable, i want the player to ‘stick’ to the ground instead of gliding down.

How can i achieve this?

This is my current movement script:

public float moveSpeed;
    public float jumpHeight;

    private bool grounded;
    public Transform groundCheck;
    public float groundCheckRadius;
    public LayerMask groundObjects;
    private Rigidbody2D rigidBody;

    // Use this for initialization
    void Start () {
        grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundObjects);
        rigidBody = GetComponent<Rigidbody2D>();
    }
   
    // Update is called once per frame
    void Update () {

        grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, groundObjects);

        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
            if (!(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))) {
                rigidBody.velocity = new Vector2(moveSpeed, rigidBody.velocity.y);
            } else {
                rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
            }
        }

        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
            if (!(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))) {
                rigidBody.velocity = new Vector2(-moveSpeed, rigidBody.velocity.y);
            } else {
                rigidBody.velocity = new Vector2(0, rigidBody.velocity.y);
            }
        }

        if ((Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.UpArrow)) && grounded) {
            rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpHeight);
        }
    }

Kind regards and have a nice weekend!

I guess you can make the player check for the signed angle of the ground using a raycast, and modifying the velocity (instead of going down while not moving you have to be going perpendicular to the ground) (instead of going right you have to be going parallel to the ground). I guess it needs trigonometry.

That could be an option.
By the way, a good reference of how i want the movement to be is like
Ori and the Blind Forest

That’s a good view of how i see my movement to be.

I found my code working good ! :slight_smile: Try it. You should have it in FixedUpdate…

        int keyW = Input.GetKey (KeyCode.W) ? 1 : 0;
        int keyS = Input.GetKey (KeyCode.S) ? 1 : 0;
        int keyA = Input.GetKey (KeyCode.A) ? 1 : 0;
        int keyD = Input.GetKey (KeyCode.D) ? 1 : 0;

        rb2d.velocity = new Vector2 ((keyD - keyA) * speed, (keyW - keyS) * speed);

jamertwo, actually, this doesn’t apply to DSG’s project unfortunately.

If you’re using unity’s 2D physics and you have gravity you can use addforce instead of modifying the velocity directly, this will take into account gravity and drag which may get you the results you’re looking for.

Thanks, I forgot to say that this is for top-down games :wink: :slight_smile:

I tried this, but the gravity still pulls me down the slope, and addForce creates an exponential movement speed (When pressing D, you slowly accelerate) instead of instantly moving a constant speed.

You are pulled down because of the gravity. You’ll have hard time climbing the slopes, because your character is like trying to go through a wall (a part of your x velocity is turned into y velocity, the rest of it is eaten by the wall). You’ll have “little jumps” when you descend slopes because you are trying to go left/right instead of going along the slope. There are tutorials around youtube .

This channel explain (not perfectly) how to do that. you can try to understand how it works and try to use it your way.