我在下面有一个基本代码。我正在尝试让玩家不要在斜坡上滑下。我的坡度现在是 45°。如果玩家在斜坡上停止移动,它会滑下来(可能是因为velocity.y += delta * gravity.y
)。当玩家在斜坡上时,我可以通过正常获得角度并设置velocity.y = 0
它不会滑下。但我不确定这是否是最好的方法。您对我如何实现这一目标有任何想法吗?顺便说一句,有没有办法在 gdscript (即 d efault_gravity
)上获取 project_settings 值?
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