Velocity.bounce not working on iterated object

Godot Version

4.1.3

Question

I used to have a player character bounce on collision, after some design I made the player node an iterated object for easier multiplayer but now the bounce doesn’t work anymore.

Relevant code:

Player movement and collision detection:

func _physics_process(delta):
	playermvt(delta)
	move_and_slide()
	if get_slide_collision_count() >= 1:
		var collision = get_slide_collision(0)
		if collision != null:
			velocity = velocity.bounce(collision.get_normal())
			print("collision detected, should bounce ")
		



func get_input():
	input.x = int(Input.is_action_pressed(playerIndex+"_right")) - int(Input.is_action_pressed(playerIndex+"_left"))
	input.y = int(Input.is_action_pressed(playerIndex+"_down")) - int(Input.is_action_pressed(playerIndex+"_up"))
	return input.normalized()	

func playermvt(delta):
	input = get_input()

	if input == Vector2.ZERO:
		if velocity.length() > (friction*delta):
			velocity -= velocity.normalized() * (friction*delta)
		else:
			velocity = Vector2.ZERO
	else:
		velocity += (input * accel * delta )
		velocity = velocity.limit_length(max_Speed)

Iteration (currently on button press):

if Input.is_action_just_pressed("playerspawn"):
	print("spawned player")
	var spawn = instplayer.instantiate() 
	add_child(spawn)

I’ve put a print on where the bounce happens and it shows up, idk why it’s not actually bouncing.

What’s the values of velocity before bounce, velocity after bounce, and collision normal?

Console output on a collision:
velocity pre bounce (-604.8787, 1.1437)
collision.get_normal() = (0.001891, 0.999998)
velocity post bounce (-604.8787, 1.143697)
collision detected, should bounce

Judging from values your normal is almost perfectly vertical in downward direction (so it’s a ceiling) and your velocity is almost horizontal BUT ever so slightly going down as well.

A body can’t bounce of off a ceiling if it is already going down.

If you flip Y component of the normal (making it into a floor), then the velocity will properly bounce.

BTW, you are using only the first collision, there may be many.
Try replacing this part:

	if get_slide_collision_count() >= 1:
		var collision = get_slide_collision(0)
		if collision != null:
			velocity = velocity.bounce(collision.get_normal())
			print("collision detected, should bounce ")

with this:

	for i in get_slide_collision_count():
		var collision = get_slide_collision(i)
		if collision != null:
			velocity = velocity.bounce(collision.get_normal())

It doesn’t work it’s so weird

Does it work if you release input keys just before hitting a wall?
I suspect that your acceleration/deceleration may override the bounce.

pretty much half of those do that, it doesn’t work either way, I am really lost.

for information the bug started when I turned the player node that was in the world node into an instantiable object that I spawn manually.

My issue was that I was using grounded motion mode instead of floating, idrk what it changes but issue resolved lmao

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.