More ideas of how to prevent slope slide down with KinematicBody2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gcivico
:warning: Old Version Published before Godot 3 was released.

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
:bust_in_silhouette: Reply From: gcivico

I was reading this (https://forum.godotengine.org/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.

:bust_in_silhouette: Reply From: avencherus

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.)

… 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.

gcivico | 2017-07-28 18:00

:bust_in_silhouette: Reply From: gcivico

This is a good approach too (physics - How do I make a box don't slide up/walk normally on a slope? - Game Development Stack Exchange)