Rotation issues when hitting a wall and assigning new direction

Godot Version

Godot Version 4.2.1

Question

Hello, I had a question about why my rotation is being weird when I change it to be the direction of the velocity after hitting a wall. The code for that is here:

if movement and "Wall" in movement.get_collider().get_parent().name:
	velocity = velocity.bounce(movement.get_normal())
	rotation = deg_to_rad(velocity.angle())

And therefore due to this problem when I try to change my direction for the random movement it seems the rotation is unchanged and it causes the agent to keep on coming back in the general direction of the wall as the rotation is not working and so it cannot get a new direction away from the wall and continue into it.

My code for randomly changing the direction, which every 1-3 seconds via another function:

func random_direction():
	rotation = randomdir.randf_range(get_parent().rotation-45, get_parent().rotation+45)
	velocity = Vector2.RIGHT.rotated(deg_to_rad(rotation))
	_state = States.random_moving

Any suggestions on how to fix this would be helpful.

You’re doing a degrees to radians on a radian.

I assume you are talking about the top code, but even when I change that over to rad_to_deg. It is still having issues.

In

rotation = deg_to_rad(velocity.angle())

Angle() returns radians, so running it thru deg_to_rad is most likely messing you up.

I think it is something due to the fact that the rotation is never really changed when it hit the wall and so after bounding off it comes back like it was doing before.

Even when I try this it doesn’t work as well. Surely when I change the rotation when it hits the wall it should remain that rotation when it next checks random dir it should head in the same general direction and shouldn’t pull a 180 after running the function?

Sounds like the bounce isn’t getting applied, or maybe something’s overwriting it?
I’d suggest adding some print statements to help you debug.

1 Like

After just using radians to make it more simple and adjusting some other things I was finally able to make it work. Thanks for your suggestions. :smile:

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