Simplest way to get range of a Vector3 Value?

Godot Version

Godot Version 4.5.1 Stable

Question

Hello! I am trying to write a script to make my player jump in the opposite direction of the walls normal, but unless the normal is EXACTLY the correct value it doesn’t work.

if jump_pressed and jump_charges > 0:
		if is_on_wall() and get_wall_normal() == -move_direction:
			
            jump_charges += 1
			jump()
            #Make player jump slightly away from their move direction
			print("Jumped off wall you're facing")
			velocity.x = get_wall_normal().x * 10
			velocity.z = get_wall_normal().z * 10

		else: jump()

I tried changing the if statement to a multiply the Vector 3 to add a buffer, but it gives odd results.
if is_on_wall() and -get_wall_normal() >= move_direction * 0.8 and -get_wall_normal() < move_direction * 1.2 :

How should I approach this? When comparing the Vector 3s I just need the X and Z values, but don’t know how to allow them to have a little buffer.

Heres a video example showing what my code does:

Check if the angle between direction and normal is under certain threshold.

Dot products offer a way to determine unit vector similarities, assuming move_direction is normalized too.

var dot := get_wall_normal().dot(move_direction)

if dot < -0.8:
    #jump

But what is -0.8? the dot product can be converted into radians using acos(x.dot(y)), though you may find better performance using cos on your desired angle as it’s a constant that can be computed once rather than every time you jump.

const MIN_WALLJUMP_ANGLE = cos(deg_to_rad(45))

var dot := get_wall_normal().dot(move_direction)
if dot < -MIN_WALLJUMP_ANGLE:
    #jump

https://www.wolfire.com/blog/2009/07/linear-algebra-for-game-developers-part-2/

3 Likes

That was just what I needed! Thank you so much!

1 Like