pedka
October 13, 2024, 4:58pm
1
Continuing the discussion from Character bouncing when moving down a slope :
Godot Version
4.3
Question
It walks up the slope at the intended speed of 250, but when it goes down it’s slower than that. Here’s the code:
extends CharacterBody2D
const SPEED = 250.0
# var speed_multiplier = 1.0
const JUMP_VELOCITY = 400.0
# var jump_velocity_multiplier = 1.0
func _physics_process(delta) -> void:
# Gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Walking.
var walk_input = Input.get_axis("ui_left", "ui_right")
if is_on_floor():
velocity = velocity.move_toward(get_floor_normal().rotated(deg_to_rad(90)) * walk_input * SPEED, delta * 720)
else:
velocity.x = move_toward(velocity.x, walk_input * SPEED, delta * 360)
# Jumping.
if Input.is_action_pressed("ui_accept") and is_on_floor():
velocity += (Vector2.UP + get_floor_normal()) / 2 * JUMP_VELOCITY
move_and_slide()
print(velocity.length())
Any ideas how to fix it? The floor is a Polygon2D
if that matters. Thanks!
Don’t know if it has anything to do with your problem. But gravity only affects the .y part of velocity.
So velocity.y += get_gravity() * delta.
Do you have “Constant Speed” checked on your CharacterBody2D?
pedka
October 14, 2024, 10:17am
4
get_gravity()
returns a vector
pedka
October 14, 2024, 10:18am
5
what do you mean by that? doesnt the last line of my script do that?
It’s a property of CharacterBody2D, by default it is off, when on the player will move up and down slopes at the same speed as flat ground.
2 Likes
Like @gertkeno said, there’s some settings you can play with that will alter this behavior under the Floor section. What you’re saying sounds like the opposite problem. You should go down faster on slopes, not slower if Constant Speed is checked. I recommend playing with the Max Angle and Snap Length values.
2 Likes
pedka
October 24, 2024, 1:15pm
8
works like a charm, and i dont even need that whole get_floor_normal().rotated(deg_to_rad(90)
transformation. it just works out of the box with velocity = Vector2.RIGHT * walk_input * SPEED
1 Like
system
Closed
November 23, 2024, 1:15pm
9
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.