Targeting camera to a certain part of an imported mesh

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

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.

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

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

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

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

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).

4 Likes

@trevordev

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

This worked also fine for me:

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:

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.