I have the base code below. I try to prevent the player from sliding on the slopes. Now my slopes are 45 °. If a player stops moving on a slope, he will move down (possibly due to velocity.y += delta * gravity.y). I can get the angle normal and set velocity.y = 0when the player is on a slope, and he does not move down. But I'm not sure if this is the best approach. Do you have any ideas on how I can achieve this? By the way, is there a way to get project_settings values in gdscript (i.e. D efault_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
source
share