I have a basic code below. I don’t think the player will slide down on the slope. My slope is now 45°. If the player stops moving on the slope, it will go down Sliding (probably because of velocity.y = delta * gravity.y). I can get the angle by normal and set velocity.y = 0 when the player is on a slope it won’t slide down. But I’m not sure if this is true The best way. Do you have any ideas on how I can do this? By the way, is there a way to get the project_settings value (ie default_gravity) on gdscript?
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
< div class="content-split">
Basically, in physical conditions, we should regard gravity as a force that will always exist.
< p>But in the game, constant gravity is not required, so you can stop it when the player is grounded and jumping or restarting when you are no longer grounded.
The method I like to use is Only clamp the speed on the y-axis so that it will never reduce the speed you use in move_and_slide() when grounding, and allow the acceleration of gravity to continue to affect it.
Note: This answer is based on Godot 3. API and the problem is in Godot 2, because I think it makes more sense.
I have a basic code below. I don’t think the player will slide on the slope. Come down. My slope is now 45°. If the player stops moving on the slope, it will slide down (probably because velocity.y = delta * gravity.y). I can get the angle by the normal and set the velocity.y = 0 It will not slide down when the player is on a slope. But I am not sure if this is the best way. Do you have any ideas on how I can do this? By the way, is there a way to get the project_settings value (ie default_gravity) on gdscript?
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
Basically, in physical situations, we should regard gravity as a force that will always exist.
But in games, constant gravity is not required, So when the player is grounded and restarting when jumping or when you are no longer grounded, you can stop it.
The method I like to use is to clamp the speed only on the y axis so that it is grounded The time will never reduce the speed you use in move_and_slide() and allow the acceleration of gravity to continue to affect it.
Note: This answer is based on Godot 3 API and the problem is in Godot 2, because I think so Will make more sense.