Move_and_slide against walls causes choppy movement

Godot Version

4.3

Question

So I have a scene set up with a characterBody2D which follows the mouse when I click and drag it. It works well, until the object collides with a wall, and I drag the mouse around on the other side of the wall. Here’s a video to show what I mean:
https://youtu.be/KriTEMV9Zs0

“Motion Mode” is set to “Floating”

“Wall Min Slide Angle” is set to 15 degrees. When I do this with it set to 0, the object moves up and down very rapidly, too quickly to record properly.

The code that governs this movement is as follows:

func _physics_process(delta):
    #set velocity equal to vector from object to mouse.
    #grabOffset = get_local_mouse_position() when object is initially clicked on. Keeps the relative mouse position consistent
	velocity=(get_global_mouse_position()-grabOffset-global_position)

    #Otherwise object moves very slowly. Issue persists if line is removed
	velocity = (velocity.length()/4)*velocity
		
    #Limits object velocity
	if velocity.length()>3000:
		velocity = 3000*velocity/velocity.length()
		
	move_and_slide()

Cannot for the life of me figure out why this happens. Any help is very much appreciated.

My guess would be your velocity is getting extremely high; going off of the video, it seems like it moves in little bits along the wall when the mouse is close, and jumps when it’s further away.

You basically have an object moving at max velocity, colliding with the wall, then the engine continues to slide with whatever velocity it has left.

1 Like

Yeah, I figured it was something like that. Do you have any suggestions on how to fix this? I haven’t been able to come up with anything short of writing my own collision handling.

I used the same code as you did, and just changing the Motion Mode to Grounded helps to mitigate the problem - see below the demo. Seems like with the Floating mode, the node sticks to the wall.

Yeah, this works. I think i switched it to floating to fix some other behaviour, but I forget what it was. For now, everything seems fine so I’ll consider this solved. Isn’t floating supposed to be used for top-down games though? Really weird that it’d completely change the behaviour like this.