Rotating vector 3 around all axis rather than just the one

Godot Version

4.0

Question

Is there anyway to make it so that the vector can rotate around every axis, rather than just y?

var hrot = get_parent().global_transform.basis.get_euler().y
	
	#input strength
	direction = Vector3(Input.get_action_strength("left") - Input.get_action_strength("right"),Input.get_action_strength("up") - Input.get_action_strength("down"),0).rotated(Vector3.UP, hrot)

This works, as long as the object isn’t facing up or down.

I’ve been losing my mind over this, so excuse me if I can’t explain my problem very well.

Essentially, I have a parent node, which follows a track, and it has a kinematicbody3d as a child, which the snippet of code above is attached to. If the parent node is perfectly level, the code above works just fine, regardless of how it’s rotated on the y axis. However, if it’s rotated around the x axis, the up and down movement stops working as intended.

I just need a solution that works like the rotated() function as written above, where it rotates the vector3 inputs to match the parent object’s y rotation, but for all axis, rather than just the one. Hope that makes sense.

Rotate around global_basis.y instead of Vector3.UP. Also the input axes should probably be assigned to x and z components of a Vector3, not to x and y, but this really depends on what exactly you’re trying to achieve.

If you’re just trying to transform the input into some node’s basis, you don’t need to rotate anything, just multiply the 3D input vector with node’s global basis.

Thanks for replying. I am trying to transform the input to a node’s basis.

When typing “global_basis.y”, I get an error saying “global_basis is not declared on current scope”

So I changed that bit of code to this:

```direction = Vector3(Input.get_action_strength(“left”) - Input.get_action_strength(“right”),Input.get_action_strength(“up”) - Input.get_action_strength(“down”),0) * get_parent().global_transform.basis```

And now when the parent object is rotated 90 degrees on the y axis, the left and right movement is reversed. I’m sure I’m missing something obvious.

global_basis was added in 4.2 I think, but yeah you can use global_transform.basis instead. Have in mind that matrix/vector multiplication is not commutative. Different order of operands will have different results. To transform from basis to global space, you want basis * vector.

direction_local = Vector3(Input.get_axis("left", "right"), 0.0, Input.get_axis("up", "down"))
direction_global = get_parent().global_transform.basis * direction_local

1 Like

That did it, thank you so much!

1 Like