I feel a little dumb asking about this, but this problem has me stumped. In my 3.5 project, I used this code for a few different things that could both bounce and move along the ground. It worked great for my purposes because they could also detect is_on_floor or is_on_wall which (as far as I understand) you can't do with move_and_collide.
I've been struggling to replicate the same behavior in 4.0.3 though. The major problem is that objects will not bounce and just stop dead once they collide with a surface.

For comparison, this is the 3.5 code.

    func _physics_process(delta) -> void:
	var _velocity = move_and_slide(velocity, Vector2.UP)
	if get_slide_count() > 0:
		var collision = get_slide_collision(0)
		if collision != null:
			velocity = velocity.bounce(collision.normal)

And this is the 4.0 code.

    func _physics_process(delta) -> void:
	move_and_slide()
	if get_slide_collision_count() > 0:
		var collision = get_slide_collision(0)
		if collision != null:
			velocity = velocity.bounce(collision.get_normal())

I made 2 minimal demo projects today, one in 3.5 and one in 4, to try and pin down the problem and it seems like the issue is how move_and_slide works in Godot 4. In 3.5 I was just discarding the return value instead of feeding it back into move_and_slide. However, if I changed the line that calls move_and_slide to velocity = move_and_slide(velocity, Vector2.UP) it stops dead on collision just like it does in 4. From the documentation I've read, it looks like 4 feeds velocity back into move_and_slideautomatically. Is there any way to override this behavior? It's also possible I've overlooked some other parameter or setting in 4.0. Any advice would be great. Thanks for reading!

  • xyz replied to this.
  • mirai Store the velocity vector in a temp variable prior to calling move_and_slide(). If collision happened, bounce that temp vector instead of actual velocity vector that's been altered by move_and_slide()

    mirai Store the velocity vector in a temp variable prior to calling move_and_slide(). If collision happened, bounce that temp vector instead of actual velocity vector that's been altered by move_and_slide()

      xyz Thank you so much! This worked perfectly! Can't believe I was tearing my hair out over something so simple.