#When sliding on a slope it does not maintain inertia and I am going crazy
12 messages · Page 1 of 1 (latest)
Now i made a new project to develop this mechanics, i adjunt a video with the scene.
can you show the whole code of the characterbody? the default one is intended to prevent sliding.
I think you can fix this by setting velocity to real velocity if the surface the player was touching had an angled surface. I think the functions were called get_floor_normal() and get_real_velocity(). If you have any code that like stops the player in the x direction you’ll also have to prevent that from running while the player is on a slope
I can’t really remember rn but I think if the player has 0 velocity on an angled surface you need to set the velocity to like -1 or 1 so that the player can start accelerating. You might also need to change what angle is considered a floor in the character body node settings.
Alternatively using the floor angle you know when the player is touching a slope so you can code your own thing where the player starts falling and accelerating in the x direction. This might be better because then it’s probably easier to copy jump king
the main problem here is the fact that sliding doesn't actually change the direction the velocity property has.
i might suggest to use a rigid body instead.
actually, sliding doesn't seem to affect the velocity at all.
the only way the velocity seems to be automatically changed is when motion mode is "grounded" and either landing on the ground or bumping on the ceiling.
ok i got it working and i dont think you need to change any of the character body settings
extends CharacterBody2D
var speed = 300
var jump_power = -700
var gravity = 1100
var gravity_limit = 1100
var drag = 4000
var stunned = false
var slide_speed = 2
var sliding_speed_limit = 900
func _physics_process(delta):
# Regular Movement Code. Not based from jump king
velocity.y = min(velocity.y + gravity * delta, gravity_limit)
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = jump_power
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * speed
# If this is applied at all times then you will instantly stop
# In Jump King I think this is applied when on floor
elif round(get_floor_angle() * 180 / PI) != 45 and not stunned:
apply_friction()
move_and_slide()
# Check if floor is slope
if round(get_floor_angle() * 180 / PI) == 45:
# Stun player. Use this to prevent movement and stuff
stunned = true
# Change Player velocity to follow slope
velocity = get_real_velocity()
# Sliding Speed Modifier
var acceleration = (delta * slide_speed) * max(velocity.length(), 1)
# Get Direction of player velocity
var dir = 1
if velocity.x < 0:
dir = -1
# Increase Velocity depending on acceleration and limit it
if velocity.length() < sliding_speed_limit:
velocity = Vector2(velocity.x + dir * acceleration, velocity.y + acceleration)
# If velocity is 0 Set vel to 1 or -1
# depending on which way slope is facing
if get_floor_normal().x > 0.1 and round(velocity.x) == 0:
velocity.x = 1
elif get_floor_normal().x < -0.1 and round(velocity.x) == 0:
velocity.x = -1
# When we land unstun the player
elif is_on_floor():
if stunned:
# Play SPLAT animation
pass
stunned = false
func apply_friction():
velocity.x = 0