Godot Version
4.5
Question
Hi, I’ve got this weirdly inconsistent behavior when moving the player object. Basically, when the player collides with a StaticBody2D, it slides across its edges, but only in one direction. I’ll explain in more detail below.
I cannot attach a couple of videos I have prepared as an example, so I’ve uploaded them to an external site.
First: https://imgur.com/QsaJrLz
This is the issue I experience. When the player hits the static body, if I press the key for left movement, it slides along the edge of the body, to the left and up. However, when I press the key for down movement, it doesn’t slide down and right, it does not move at all. (And when I move right, it just moves away from the body, not illustrated, since that is what is supposed to happen).
The player is a CharacterBody2D, parent of an AnimatedSprite2D (with the iconic icon.svg as its texture), a CollisionShape2D (the blue circle), and a RemoteTransform2D. Only the CharacterBody has a script, which I have included at the end. The obstacle is a StaticBody2D, with a CollisionPolygon2D, in build mode “Solids”. The polygon is a PackedVector2Array.
Second situation: https://imgur.com/gTw7A4i
In the second case, in the same scene with the same components, it works different, which is the behavior I’d like to implement. In this case, when facing a different wall, when pressing the key to move up, the player slides up and right; and when pressing the key to move left, it slides left and down. So it slides both ways against the wall both ways, instead of just one. The StaticBody has 6 different sides, and this is the only one when that happens with a single key pressed.
On any edge, if I press two keys for diagonal movement, it does slide along the edge.
This is the player script, with very little logic of my own. Godot’s builtins are doing all the work:
extends CharacterBody2D
@export var speed: int = 900
func _ready() -> void:
pass
func get_input():
if Dialogic.current_timeline != null:
return # if dialogue is playing dont move
var input_direction = Input.get_vector("left", "right", "up", "down")
velocity = input_direction * speed
func _physics_process(delta):
get_input()
move_and_slide()
How could I have it so the player is always able to slide against static bodies? Also, I’m curious about why this happens in the first place.
Thanks!