+1 vote

I have a basic code below. I am trying that the player not slide down on slopes. My slopes now are 45°. If the player stop movement on slope, it will slide down (maybe because velocity.y += delta * gravity.y). I can get the angle by normal and set velocity.y = 0 when player is on slope and it will not slide down. But I am not sure if this is the best approach. Do you have any ideas of how I can achieve this? By the way, is there a way to get project_settings values on gdscript (ie. default_gravity)?

extends KinematicBody2D

var gravity = Vector2(0,700)
var velocity = Vector2()
var hSpeed = 150
var onSlope = false

func _ready():
    set_fixed_process(true)
    pass

func _fixed_process(delta):
    var left = Input.is_action_pressed("ui_left")
    var right = Input.is_action_pressed("ui_right")

    if left:
        velocity.x = -hSpeed
    if right:
        velocity.x = hSpeed
    if !left && ! right:
        velocity.x = 0

    velocity.y += delta * gravity.y
#   if onSlope && !left && !right:
#       velocity.y = 0
#   else:
#       velocity.y += delta * gravity.y

    var movement = delta * velocity
    move(movement)

    if is_colliding():
        var normal = get_collision_normal()
        var angle = getAngleByNormal(normal)

#       I can get the angle here
#       if angle == 0 player is on ground
#       if abs(angle) > 0 && abs(angle) < 90 player is on slope |> onSlope = true

        velocity = normal.slide(velocity)
        movement = normal.slide(movement)
        move(movement)
    pass


func getAngleByNormal(normal):
    var inverseNormal = normal * -1
    var angle = inverseNormal.angle()
    angle = round(rad2deg(angle))
    return angle
    pass
in Engine by (148 points)
edited by

3 Answers

0 votes

I was reading this (https://godotengine.org/qa/3165/2d-platformer-movement-on-slopes?show=3165#q3165) but the final solution I think is not what I want because if I change the slope's inclination, I have to change the gravity force (gravity.y) according to the slope. Maybe there are other ideas of how to play around this problem.

Any idea you have will be great !!

Thanks in advance.

by (148 points)
0 votes

There are probably a lot of solutions, and many that would only work in very specific situations. As long as you have something that does what you want with no side effects, that will do.

The idea I would offer, is that if you want things to cling to slopes, you would cast gravity in the direction of the collision normal of the surface.

Check your angle of whatever you're colliding with the surface, then take the collision normal and multiply it by your gravity. This will be your new gravity vector.

Now when they jump you likely want to resume doing gravity normally, but if your game is something where you're using gravity boots, maybe you check a flag for gravity boots, and continue applying that surface normal. (With some additional checks for other things.)

by (5,203 points)

... you would cast gravity in the direction of the collision normal of the surface.

I didn't think in that. Thanks for the idea. I will try to implement it.

0 votes

This is a good approach too (https://gamedev.stackexchange.com/a/114326)

by (148 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.