# Targeting camera to a certain part of an imported mesh

**URL:** <https://forum.babylonjs.com/t/targeting-camera-to-a-certain-part-of-an-imported-mesh/1037>\
**Category:** Questions\
**Created:** [January 29, 2019, 6:08pm UTC](https://forum.babylonjs.com/t/targeting-camera-to-a-certain-part-of-an-imported-mesh/1037 "2019-01-29T18:08:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Modos](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/modos/32/317_2.png) [@Modos](https://forum.babylonjs.com/u/Modos)\
**Post date:** [January 29, 2019, 6:08pm UTC](https://forum.babylonjs.com/t/targeting-camera-to-a-certain-part-of-an-imported-mesh/1037/1 "2019-01-29T18:08:39Z")

</div>

Hey guys²

I will spam the forum now with a few threads, because I think it would be too much if I summarize everything in one.

> **Third-Person View**
>
> Here, **I want to target the camera to the head of my character** or at least something higher than the feet. If you zoom in, you realize, the bottom of the character is the target of the camera.  
> With the scaling-function (_line 146_) I could scale the head with _skeleton.bones[4]_, but I can’t target the head with _camera.target = skeleton.bones[4]_ and I don’t know how to get the camera to focus the head or shoulders.

> **First-Person View**
>
> Of course **I want the FreeCamera also on an higher place**. If you change the view and look above, you can see the upper part of the character.  
> I already tried to change that with _camera2.ellipsoid = new BABYLON.Vector3(1, 2, 1);_ after line 125, but this also doesn’t seem to work.

**The view can be changed by pressing v.**

Example:  
**[https://www.babylonjs-playground.com/#BCU1XR#321](https://www.babylonjs-playground.com/#BCU1XR#321)**

I have the feeling the solution isn’t that hard, but I can’t find it.  
I hope you guys can give me some ideas.  
Thank you in advance.

---

<div class="post-metadata">

**Author:** ![trevordev](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/trevordev/32/16_2.png) [@trevordev](https://forum.babylonjs.com/u/trevordev)\
**Post date:** [January 29, 2019, 7:09pm UTC](https://forum.babylonjs.com/t/targeting-camera-to-a-certain-part-of-an-imported-mesh/1037/2 "2019-01-29T19:09:28Z")

</div>

For skeletons, you can attach a mesh to the skeleton and look at that, see: [https://www.babylonjs-playground.com/#11BH6Z#396](https://www.babylonjs-playground.com/#11BH6Z#396)

For first person you could try manually update the position yourself to hire up, eg:

```auto
scene.onBeforeRenderObservable.add(()=>{
    camera2.position = newMeshes[0].position.add(new BABYLON.Vector3(0,10,0))
})

```

---

<div class="post-metadata">

**Author:** ![timetocode](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/timetocode/32/111_2.png) [@timetocode](https://forum.babylonjs.com/u/timetocode)\
**Post date:** [January 29, 2019, 10:24pm UTC](https://forum.babylonjs.com/t/targeting-camera-to-a-certain-part-of-an-imported-mesh/1037/3 "2019-01-29T22:24:13Z")

</div>

Certainly there are a lot of possibilities, but I see the cameras as being somewhat separate from the player model.

For first person the camera is inside (or at the front) of the head but doesn’t necessarily stick to any of the bones. For me that would make the base mesh its parent so that it moves along with it, but then at a position hardcoded to be at the correct height. Also every frame I would copy the `rotation.y` between the camera and the mesh so that the camera and the player mesh both face the same direction. If it is a multiplayer game then perhaps the head (just the head!) should also rotate in the x direction so that other players can see people look up and down. In singleplayer I would just totally remove/hide the head while in first person because it would be invisible anyways.

pseudocode

```auto
camera.setParent(playerMesh) // or parent = playerMesh, can't rememebr
camera.position.y = 8 // or something
// optionally hide the head
// then every frame...
function fakeUpdateExample() {
   playerMesh.rotation.y = camera.rotation.y // or the other way around
   head.rotation.x = camera.rotation.x // hypothetical
}

```

For third person everything is similar except the camera isn’t floated at the head/face and instead is floated on an invisible pole above and behind the player. Similarly to before I perceive this relationship as being the camera parented to the mesh, and then hardcoded to a position that offsets it appropriately. I believe this will cause it to magically (and rigidly) remain behind the player without any custom logic each frame.

That’s just the position of the camera, its rotation on the other hand requires some math. Maybe something like moving the hypothetical first person camera’s position 100 units forward and then calling `thirdPersonCamera.lookAt(positionWayInfrontOfHead)`.

---

<div class="post-metadata">

**Author:** ![Modos](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/modos/32/317_2.png) [@Modos](https://forum.babylonjs.com/u/Modos)\
**Post date:** [February 1, 2019, 4:10pm UTC](https://forum.babylonjs.com/t/targeting-camera-to-a-certain-part-of-an-imported-mesh/1037/5 "2019-02-01T16:10:47Z")

</div>

@trevordev

> [@trevordev](#):
>
> For skeletons, you can attach a mesh to the skeleton and look at that

Nice, it is mostly the same solution as the one in my other thread:

> [@Problem with infinite rays and position of raycast](https://forum.babylonjs.com/t/problem-with-infinite-rays-and-position-of-raycast/1036/2):
>
> You are generating a ray per frame in your castRay() function. This function creates an helper which in turn generate a mesh (per frame again). So no wonder why it is slow slight_smile Why are you generating a ray per frame? (scene.registerBeforeRender is called once a frame) You can create a dummy box, attach your box to the bone with mesh.attachToBone and then attach your ray to this dummy box

This worked also fine for me:

```auto
scene.onBeforeRenderObservable.add(()=>{
    camera2.position = newMeshes[0].position.add(new BABYLON.Vector3(0,10,0))
})

```

@timetocode  
Thank you for your explanations and ideas.  
I’m planing to make a FPS, so it is supposed to be a multiplayer game.  
You have a good point with moving the head with the camera, I didn’t think of that.

Apparently I can’t rotate a bone on its own.  
I tried:

```auto
botHead = skeleton.bones[4]
botHead.rotation.x = -2; // the same with x/z and other numbers 

```

But at least the whole mesh looks in the direction of the mouse now (still two dimensional)  
Even in first person.

> **[Babylon.js Playground](https://www.babylonjs-playground.com/#BCU1XR%23340)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes
