Godot Version
v4.2.2.stable.official [15073afe3]
Information
The behavior is hard for me to explain in words, so I’ll attach a video as well as try to describe it.
I have a CharacterBody2D
with a CollisionShape2D
circle as a character. I also have a StaticBody2D
with a few CollisionShape2D
s as a level.
The character is coded to launch itself in the direction of the mouse when hitting the button; it just adds velocity in that direction on the moment the button is pressed. There’s a bunch of code, but I tested with nothing but the few lines needed for that and the behavior still happens.
The Behavior
When hitting a corner (the hard edge of a CollisionShape2D
rectangle, or the rounded end of a CollisionShape2D
capsule shape), the player slowly slides along the corner or the curve until it eventually stops, at a seemingly random point.
With the basic code, the velocity
remains unchanged as this happens, which is what normally happens in a collision between a CharacterBody2D
and a StaticBody2D
.
However, I have a bit of code that sets the velocity
to (0, 0) when is_on_wall()
returns true (Motion Mode
is set to floating
, so everything counts as a wall):
Using print(velocity)
and print(get_real_velocity())
I can see that, even while the character is doing the weird slide, the velocity is 0. The “real velocity” isn’t 0, but that’s not as weird to me as the character moving while the velocity
is (0, 0).
Additionally, the slide will happen in a direction I don’t know how to predict. The character can be moving up and to the right when it hits the corner, and slide down and to the left.
I was not able to replicate this when setting the CharacterBody2D
’s CollisionShape2D
to a square or to a capsule.
The Code
I tested with nothing but this code running, and the problem persisted:
const SPEED = 1200
var mouse_position = null
func _physics_process(delta):
mouse_position = get_global_mouse_position()
var direction = (mouse_position - self.position).normalized()
if Input.is_action_just_pressed("up") and get_real_velocity() == Vector2.ZERO:
velocity = direction * SPEED
move_and_slide()
The Video
In the video, the only thing I was doing was moving the mouse, and pressing (and releasing) “W”:
(the raycasts are just there for testing and debugging, to be clear)
This is my first ever game I am trying to make, please do let me know if there’s something I’m visibly doing wrong that’s causing this. To me it seems like a bug with the engine, but being so new to it, I really can’t know that for sure.
Thanks!