How to rotate a mesh (instanced mesh) towards direction?

I am working on saving/loading of flying rockets. I can restore it and make it move towards right direction, but I can’t turn it correctly. The front of rocket should be oriented towards flight direction. Direction might be oriented to anywhere in 3D space.

I understand that this should be very easy. But I am completely stuck on it.
https://playground.babylonjs.com/#FVYPXL

Use the .lookAt method.
Although it looks like it assumes the orientation of the cylinder to be in the side. Applying a rotation on the X axis will fix that though.

Just add this to the end of your example:

    rocket.lookAt(direction);
    rocket.rotate(new BABYLON.Vector3(1, 0 ,0), BABYLON.Tools.ToRadians(90));
1 Like

Hi @DisownedWheat,

Actually this works not as expected, because it aims rocket towards end point of direction, not to direction itself. You will see what I am talking about if you will try to put rocket into another coordinates.

But your hint helped me to implement the correct solution. Thank you!

We need to add direction vector to the current coordinates of rocket first.
https://playground.babylonjs.com/#FVYPXL#1

1 Like

Ah you’re absolutely right, I didn’t check that.
Glad you figured it out even with my incorrect help :smiley:

1 Like