![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | daniel-specter |
Hello everyone,
i have this little project for a game, a 2D endless scroller, the character moves at constant speed and the only interaction from the player is the jump.
This is the code i copypasted from various youtube tutorial,
the move_and_slide_with_snap() does i’ts job.
The only problem i have right now is when i jump and land on a slope (45° angle).
if the slope is going up the player will stop or move back for a moment and then proceed to move again, similar thing when the slope is going down, the player will move faster down for a moment, then reset the “normal” speed.
I imagine the problem is with the velocity.x and velocity.y and the angle of the ground causing the speed variation but i can’t figure out a solution.
The result i wish is the player keep moving right at a constant speed, even if going up ad down on slopes and jumping or landing on it.
Hope someone can give me some tips about this
extends KinematicBody2D
const floor_normal = Vector2.UP
const snap_normal = Vector2.DOWN
const snap_length = 18
const floor_angle = deg2rad(50)
var snap_vector = snap_normal * snap_length
var gravity = 500
const speed = 80
var jump_power = 200
var velocity = Vector2.ZERO
func _physics_process(delta):
velocity.x = speed
velocity = move_and_slide_with_snap(velocity, snap_vector, floor_normal, true, 4, floor_angle, false)
if is_on_floor():
if Input.is_action_just_pressed("jump"):
velocity.y = -jump_power
snap_vector = Vector2()
else:
snap_vector = snap_normal * snap_length
if Input.is_action_just_released("jump") and velocity.y < 0.0:
velocity.y *= 0.6
velocity.y += gravity * delta
if position.y >= 500:
#death()
get_tree().paused = true
func death():
queue_free()
Maybe you could adjust the floor_max_angle
parameter in the move_and_slide_with_snap()
function? If not, then maybe look into checking when the player is on the floor or on a slope without just using is_on_floor()
? For example, some games use a downward facingRayCast
(from the center of the player character) to detect when a player is on a slope.
Ertain | 2023-05-14 21:27
I’ve tried to adjust the foor_max_angle but seems to do nothing for my problem.
I saw the article HERE that mention about Raycasting but i have no clue on how to use it.
I’ll try to use RayCast as a collisionshape maybe it help.
daniel-specter | 2023-05-16 03:11