# How to rotate a Basis to face a specific z-direction

4.2.1 stable

### Question

Hey there, I’m struggling to understand rotations and how to rotate to face certain directions.

I’ve got a basis that can be rotated in any orientation, and an arbitrary Vector3 that I want to use as a new forward facing direction for my basis. Ideally, I’d like a shortest-path rotation.

I’m lost because I don’t know how to find the axis of rotation. It could be on the X, or Y, or both. Or maybe it’s some imaginary axis that’s a combination of both?

Any help would be appreciated! I’ve tried the built-in function looking_at(), which sounded promising, but I can’t get working.

Maybe I am getting a bit confused with the language here, but what do you mean by “Rotate” a basis. Usually people use a set of Basis vectors to represent some kind of space or transformation.

Do you mean that you choose a basis for some linear transformation, and you made your Vector3 from that transformation, and now you you would like to know how much it rotated?

I am confused by your question.

I may be confused myself, I’m just now getting into Godot and 3D programming in general.

From my understanding, a Node3D has a transform, which is made of a Basis and Origin. The Origin is its position in space, and the Basis is its orthonormalized rotation + scale. Is that correct?

For a little more detail, I have a CharacterBody3D. The movement direction, however, is different from the physical orientation of this node. To separate the movement and orientation, I created a separate transform/basis that will be used for movement (I could use a straight Vector3 for a direction, but I think a transform/basis will be better for what I want to accomplish in other areas of the project). I want to rotate this movement transform/basis, and not the node’s own transform. But instead of “adding” or “taking away” some rotation around an axis using input or something, I have a specific forward direction I want the movement transform.basis.z to actually become instantly.

I may be going at this whole thing the wrong way, but when I look at this page in the documentation, it tells me to not use Euler angle rotation and instead rotate the transform/basis.

Not sure about Godot, but definition of basis in Linear Algebra is that a basis for a space must be linearly independent and span that space. So orthonormalized + scale could be correct as orthonormal implies linearly independent.

I’ll have to look at the docs for that.

A transformation in general takes a vector from one space to another space. And so you can use a transformation on a vector to take it to somewhere else. No problem. If you want to change this transformation. In linear algebra, you would usually use a composition of matrices, e.g. Rotate then translate.

I’m not sure what the equivalent is in Godot, sure I am sure it is possible to use a transformation matrix to move your character.

I think I’ve figured out a solution to my problem. It was indeed the “looking_at()” function! On first attempt, I was entering in the wrong direction, which was causing some strange bugs. I thought I had figured out which direction to use, but apparently not until now. Thanks for your assistance!

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