# Problem when using lookAt() on an imported mesh

**URL:** <https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490>\
**Category:** Questions\
**Created:** [April 10, 2019, 9:21pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490 "2019-04-10T21:21:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 10, 2019, 9:21pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/1 "2019-04-10T21:21:06Z")

</div>

I have created a simple sphere that moves from bottom to top and then called :

> scar1.lookAt(sphere.position);

Where scar1 is the model of a weapon I imported and I’m trying to make the weapon point to the sphere when it moves, but it is positionned side way and it thus, it is not the canon that follow the sphere but the sides of the weapon…  
here is the code:

> var canvas = document.getElementById(“renderCanvas”); // Get the canvas element  
> var engine = new BABYLON.Engine(canvas, true); // Generate the BABYLON 3D engine
> 
> /\*\*\*\*\*\*\* Add the create scene function \*\*\*\*\*\*/  
> var createScene = function() {
> 
> ```
> // Create the scene space
> var scene = new BABYLON.Scene(engine);
> 
> // Add a camera to the scene and attach it to the canvas
> var camera = new BABYLON.UniversalCamera("Camera", new BABYLON.Vector3(-6.5, 0, 0), scene);
> camera.attachControl(canvas, true);
> var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(-1, -2, -1), scene);
> light.position = new BABYLON.Vector3(5, 5, 5);
> var ground = BABYLON.MeshBuilder.CreateGround("ground", {
> width: 40,
> height: 40,
> subdivisions: 4
> }, scene);
> ground.position.y = -2.05;
> ground.receiveShadows = true;
> var scar1;
> var scar2;
> var scar3;
> var sphere = BABYLON.MeshBuilder.CreateSphere("mySphere", {
> diameter: 1,
> diameterX: 1
> }, scene);
> sphere.position.x = 20;
> camera.setTarget(sphere.position);
> var assetsManager = new BABYLON.AssetsManager(scene);
> var meshTask = assetsManager.addMeshTask("scar task", "", "static/models/fn_scar-l/", "scene.gltf");
> meshTask.onSuccess = function(task) {
> console.log("success");
> scar1 = task.loadedMeshes[0];
> task.loadedMeshes[0].position = BABYLON.Vector3.Zero();
> var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
> shadowGenerator.addShadowCaster(scar1);
> camera.position.y += 2;
> //scar1.rotationQuaternion = new BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(0, 0, 1), Math.PI/2);
> 
> let t = 0;
> scene.registerBeforeRender(function() {
> let py = 25.0 * Math.cos(t / 3.5);
> sphere.position.y = py;
> t += 0.1;
> //scar1.rotationQuaternion = new BABYLON.Quaternion.RotationAxis(sphere.position, Math.PI/2);
> scar1.lookAt(sphere.position);
> 
> });
> window.addEventListener("mousemove", function() {
> // We try to pick an object
> // var pickResult = scene.pick(scene.pointerX, scene.pointerY);
> // if (pickResult.hit) {
> // var targetPoint = pickResult.pickedPoint;
> // scar1.lookAt(new BABYLON.Vector3(targetPoint.x, targetPoint.y, targetPoint.z));
> // scar1.rotationQuaternion = new BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(targetPoint.x, targetPoint.y, targetPoint.z), Math.PI/2);
> // }
> });
> }
> assetsManager.load();
> var gizmoManager = new BABYLON.GizmoManager(scene);
> gizmoManager.positionGizmoEnabled = true;
> gizmoManager.rotationGizmoEnabled = true;
> // Toggle gizmos with keyboard buttons
> document.onkeydown = (e) => {
> if (e.key == 'w') {
> gizmoManager.positionGizmoEnabled = !gizmoManager.positionGizmoEnabled
> }
> if (e.key == 'e') {
> gizmoManager.rotationGizmoEnabled = !gizmoManager.rotationGizmoEnabled
> }
> if (e.key == 'r') {
> gizmoManager.scaleGizmoEnabled = !gizmoManager.scaleGizmoEnabled
> }
> if (e.key == 'q') {
> gizmoManager.boundingBoxGizmoEnabled = !gizmoManager.boundingBoxGizmoEnabled
> }
> }
> 
> showWorldAxis(3);
> return scene;
> 
> ```
> 
> };  
> /\*\*\*\*\*\*\* End of the create scene function \*\*\*\*\*\*/
> 
> var scene = createScene(); //Call the createScene function
> 
> // Register a render loop to repeatedly render the scene  
> engine.runRenderLoop(function() {  
> scene.render();  
> });
> 
> // Watch for browser/canvas resize events  
> window.addEventListener(“resize”, function() {  
> engine.resize();  
> });

Thank you in advance for your help.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 10, 2019, 9:36pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/2 "2019-04-10T21:36:07Z")

</div>

Hello!, you can probably call something like:

```auto
scar1.bakeTransformIntoVertices(BABYLON.Matrix.RotationY(Math.PI / 2);

```

---

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 10, 2019, 9:43pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/3 "2019-04-10T21:43:30Z")

</div>

![08](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/3/30208b83318eeebb3d3ad343cb74e83c2104c572.png)  
Thanks for your answer.  
I tried to add the line, but the problem seems to remain, here is a screeshot of the scene.  
With my code becoming :

> meshTask.onSuccess = function(task) {  
> console.log(“success”);  
> scar1 = task.loadedMeshes[0];  
> scar1.bakeTransformIntoVertices(BABYLON.Matrix.RotationY(Math.PI / 2));  
> task.loadedMeshes[0].position = BABYLON.Vector3.Zero();  
> var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);  
> shadowGenerator.addShadowCaster(scar1);  
> camera.position.y += 5;  
> camera.position.z += 5;  
> camera.setTarget(scar1.position);  
> sphere.position.y = 15;  
> scar1.lookAt(sphere.position);  
> // let t = 0;  
> // scene.registerBeforeRender(function() {  
> // let py = 25.0 \* Math.cos(t / 3.5);  
> // sphere.position.y = py;  
> // t += 0.1;  
> // scar1.lookAt(sphere.position);  
> //  
> // });  
> window.addEventListener(“mousemove”, function() {  
> // We try to pick an object  
> // var pickResult = scene.pick(scene.pointerX, scene.pointerY);  
> // if (pickResult.hit) {  
> // var targetPoint = pickResult.pickedPoint;  
> // scar1.lookAt(new BABYLON.Vector3(targetPoint.x, targetPoint.y, targetPoint.z));  
> // scar1.rotationQuaternion = new BABYLON.Quaternion.RotationAxis(new BABYLON.Vector3(targetPoint.x, targetPoint.y, targetPoint.z), Math.PI/2);  
> // }  
> });  
> }

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 10, 2019, 9:49pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/4 "2019-04-10T21:49:25Z")

</div>

are you sure that scar1 is the right mesh (maybe it is one of its children)  
You can get a view of your scene tree by calling `scene.debugtLayer.show()`

---

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 10, 2019, 9:55pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/5 "2019-04-10T21:55:38Z")

</div>

![57](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/2/28c1d63dbc9477e013a0187d6fc836a480b859db.jpeg)  
I’m quite new to this so I could not tell you if if is one of its children, but here is a screen of the debugging layer.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 10, 2019, 10:08pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/6 "2019-04-10T22:08:48Z")

</div>

yep so from what I’m seeing, the gun is not the first child so scar1 = task.loadedMeshes[0] will not point to the right one

---

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 10, 2019, 10:10pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/7 "2019-04-10T22:10:55Z")

</div>

Damn it =D Am I doing this assetManager thing wrong ?  
When I modify the rotation, position, and so forth, it does work, what am I missing here ?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 10, 2019, 10:15pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/8 "2019-04-10T22:15:06Z")

</div>

it works because of inheritance but to bake a transform you need to do it on the actual mesh.  
Does it make sense?

---

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 10, 2019, 10:18pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/9 "2019-04-10T22:18:35Z")

</div>

Oh ok, it does make sense but then, is it a problem with the .gltf file in itself ?

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 10, 2019, 10:19pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/10 "2019-04-10T22:19:57Z")

</div>

nope this is totally fine to have a hierarchical structure. Perhaps you may want to better name your nodes so you can easily find them by name with `scene.getMeshByName()`

---

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 10, 2019, 10:32pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/11 "2019-04-10T22:32:30Z")

</div>

Thanks for your answer.  
Am I giving a name when doing :

> var meshTask = assetsManager.addMeshTask(“scar task”, “scar”, “static/models/fn\_scar-l/”,  
> “scene.gltf”);

Because when doing so, the gun does not appear anymore. However in the doc, it says “meshesName” for this parameter.  
Sorry for being such a noob, I started learning babylone yesterday.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 10, 2019, 11:23pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/12 "2019-04-10T23:23:29Z")

</div>

No worries. I didn’t mean to change the way you load it but more the way you reference it 🙂

Use scene.getMeshByName instead of scar1 = task.loadedMeshes[0]

---

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 11, 2019, 9:28am UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/13 "2019-04-11T09:28:31Z")

</div>

I retrieved the name by doing :

> alert(task.loadedMeshes[0].name);

The displayed name is “\_\_root\_\_”, so I wrote :

> scar1 = scene.getMeshByName(“\_\_root\_\_”);

But the gun is still turned sideways and

> scar1.bakeTransformIntoVertices(BABYLON.Matrix.RotationY(Math.PI / 2));

doesn’t seem to work.  
Here is the code :

> meshTask.onSuccess = function(task) {  
> scar1 = scene.getMeshByName(“\_\_root\_\_”);  
> scar1.bakeTransformIntoVertices(BABYLON.Matrix.RotationY(Math.PI / 2));  
> scar1.position = BABYLON.Vector3.Zero();  
> camera.setTarget(scar1.position);  
> scar1.lookAt(sphere.position);  
> var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);  
> shadowGenerator.addShadowCaster(scar1);  
> });

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 11, 2019, 1:31pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/14 "2019-04-11T13:31:58Z")

</div>

Yes but this is what I try to say. The mesh ( the real one with geometry) is not **root** but one of the children (deep down your hierarchy)

So let try something else. Keep all the code you where doing before. Remove the call to bake Transform and instead just add this line:

```auto
scene.getMeshByName("Root").rotation.y = Math. PI/2

```

---

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 11, 2019, 5:51pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/15 "2019-04-11T17:51:12Z")

</div>

Oh I see.  
I added the line but it didn’t work, and turns out :

> console.log(scene.getMeshByName(“Root”));

prints null. This is weird.

 ![37](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/7/75edcb50db54665b9d2a016ac6a9f9f1704d45d9.png)

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 11, 2019, 6:15pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/16 "2019-04-11T18:15:15Z")

</div>

my bad it is not a mesh but a transformNode 🙂  
console.log(scene.getTransformNodeByName(“Root”));

---

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 11, 2019, 8:46pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/17 "2019-04-11T20:46:21Z")

</div>

After adding the line of code, it does not work.  
Here is the code:

 ![32](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/7/75bcdc058763b20afbb7797375f1bd74939756d5.png)  
Is this way even the advised one ? Or is it better to import a .babylone file and not have these kind of problem ? I must say that it is quite frustrating.

---

<div class="post-metadata">

**Author:** ![Devin\_Wright](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/devin_wright/32/4302_2.png) [@Devin\_Wright](https://forum.babylonjs.com/u/Devin_Wright)\
**Post date:** [April 11, 2019, 9:00pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/18 "2019-04-11T21:00:07Z")

</div>

You will get it, sometimes it just takes a bit of tinkering with model files.  
For example, look at this one I threw together.  
[https://www.babylonjs-playground.com/#8LFTCH#28](https://www.babylonjs-playground.com/#8LFTCH#28)

I am having to multiply the direction of the ball by -1, since the normal lookAt uses the tail of the bird.  
There is no problem with gltf vs babylon.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 11, 2019, 9:27pm UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/19 "2019-04-11T21:27:16Z")

</div>

Also you are not helping us to help you…We need a repro in the playground. With a repro this could be fix in 10 minutes

---

<div class="post-metadata">

**Author:** ![Subzero8](https://avatars.discourse-cdn.com/v4/letter/s/e8c25b/32.png) [@Subzero8](https://forum.babylonjs.com/u/Subzero8)\
**Post date:** [April 12, 2019, 8:35am UTC](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490/20 "2019-04-12T08:35:43Z")

</div>

Yeah I wanted to make a playground but how can I put the gun model in it so you can see the problem?

[Next page](https://forum.babylonjs.com/t/problem-when-using-lookat-on-an-imported-mesh/2490.md?page=2)
