Search code examples
godotgdscript

Make bone point towards another bone in Godot using Gdscript


I want to make a bone in an armature rotate so that it's pointing towards another bone in Godot. Normally, the method void look_at ( Vector3 target, Vector3 up ) would be used, but this only works on Spatials, not Transforms (bones operate using transforms). Alternatively, if there is no method that can do this, how would I set the rotation of a transform? Instead of rotating it using the Transform rotated ( Vector3 axis, float phi ) method, is there a way to just set the rotation to something that would align with the other bone?


Solution

  • It appears you want Transform.looking_at, it will return a new Transform with the desired orientation:

    var target:Vector3 = get_target()
    var up:Vector3 = get_up_vector()
    var new_transform = old_transform.looking_at(target, up)
    

    The target vector is where the Transform should have its front towards. And the up is where the Transform should have its up towards.

    If you only have a target vector, you would have infinite possible orientations. Because you could rotate the Transform around the axis that goes from its origin to the target, and it would still be pointing towards that target.

    So you provide another vector up (which should not be in the same direction as target. Ideally it is at a quarter rotation from it) which allows you to specify the orientation around that axis.

    For example, let us say you have an airplane, and you point it towards the target. Is it flying up-side down? sideways? We don't know. We need to specify what side is up.