Vector2D direction not accurate after multiplying by multiple magnitudes

I want to make an enemy move along walls. I get the movement vector by using get_wall_normal() on base CharacterBody2D, rotating by 90 degrees if moving clockwise and -90 degrees if moving counter clockwise, then multiplying by speed. However, the final output is slightly off from what I expect it to be. For example, if the wall normal is (0, -1), I rotate by 90 degrees to get (1, 0) then multiply by 300, I’d expect to get (300, 0) but I get (300, 0.000013). I assume this is because of floating point inaccuracy, but I still don’t know how to deal with it. Multiplying (1, 0) by a large value doesn’t do this, the inaccuracy is introduced during rotation. Here is the code condensed into a line:
movement_vector = character.get_wall_normal().rotated(deg_to_rad(90)) * speed

I wasn’t able to fix the floating point error, but I solved the problem by rotating the movement vector into the wall a little bit.

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