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