Angle_to flipping when facing a specific direction

So I’ve got a Godot 4.5 script where I’m trying to move something off-center in local space depending on the angle at which a RayCast3D collides with the walls. To do this, I’ve converted Vector3s into Vector2s to use the angle_to() function, since I couldn’t figure out signed_angle_to(). Vector math is not my strong suit.

var forward_direction: Vector3 = camera.global_position.direction_to(get_collision_point())
		var forward_x = Vector2(forward_direction.x, forward_direction.z)
		var forward_y = Vector2(forward_direction.y, forward_direction.z)
		var wall_angle_x = Vector2(get_collision_normal().x, get_collision_normal().z)
		var wall_angle_y = Vector2(-get_collision_normal().y, get_collision_normal().z)
		sphere.position.x = lerp(sphere.position.x, forward_x.angle_to(wall_angle_y), 1.0 * delta)
		sphere.position.y = lerp(sphere.position.x, forward_y.angle_to(wall_angle_x), 1.0 * delta)

		sphere.position.x = clamp(sphere.position.x, -0.25, 0.25)
		sphere.position.y = clamp(sphere.position.y, -0.25, 0.25)

this seems to be working, except that when I’m facing the positive x direction, the sphere moves in the opposite direction it’s supposed to, going left when it’s supposed to go right and vice-versa; I assume there’s a similar direction I could face along the y axis that would cause it to glitch out in the y direction as well, but I’ve not noticed this, possibly because of clamped camera rotation.

I tried changing lerp() to lerp_angle() but with no success (I figured that probably wouldn’t work anyway since it was controlling linear motion, not angular). Any help would be greatly appreciated, I’ve been pulling my hair out over this for days!

What do you want to happen? Could you explain the big picture? I don’t think you will get proper 3D math by going down a dimension, if anything quaternions are 4D math to apply a 3D rotation.

I’m trying to make an object appear near the collision point of a raycast, but slightly move left if it hits a wall from the right and vice-versa, staying toward the middle if it hits a wall head-on. Sort of like bouncing, only it stays floating and almost stuck to the wall. Hopefully crudely-drawn diagram below illustrates what I’m talking about well enough.

I also want it to float slightly up and down depending on the vertical angle at which the raycast hits a wall.

Seems like you could use the wall normal

var point := get_collision_point()
var normal := get_collision_normal()
sphere.position = point + normal * distance_from_wall
1 Like

Apart from needing to use global_position, this worked! Thanks!