How to properly attach the camara to an Object?

I want my camera to be attached to my player object. (Because I’m developing a multiplayer game and handle input myself).
I tried it the naive way by setting the camera’s position to my player absolutePosition which works.
The problem with this solution is that the camera is noticeably lagging behind my player object (10-20 ingame cm) on top of that this lagging behind is sometimes more sometimes less so, my camera is kind of vibrating.

How can I attach the camera to my player without these negative side effects?

I solved something similar by setting the camera position with a
scene.onBeforeCameraRenderObservable
Or maybe try
playerMesh.getAbsolutePosition(),
which will do a
playerMesh.computeWorldMatrix()
before returning the absolutePosition

1 Like

You can parent your camera to a mesh. You might also use a transform node to gain more control. This works similar to a null in other applications.

Galen

2 Likes

Thanks. Parenting works. It’s just a little weird that I can do camera.parent = player but not player.addChild(camera). I tried the second first, that’s why I though parenting doesn’t work with cameras.

Just for completeness:
I think camera.setParent(player) is the way to go, too. But i wonder why addChild didn’t work for you as it does exactly the same thing :thinking::

abstractMesh.ts GitHub Link

/**
 * Adds the passed mesh as a child to the current mesh
 * @param mesh defines the child mesh
 * @returns the current mesh
 */
public addChild(mesh: AbstractMesh): AbstractMesh {
    mesh.setParent(this);
    return this;
}

You wrote player.addChildren(camera). player.addChildren() does not exists so it must be player.addChild(camera). Maybe that was the reason?

// Ah, you corrected your comment. Okay. I have no clue :sweat_smile:

I meant addChild. At least in the V4 Beta camera has no setParent method so mesh.addChild(camera) throws an error.

I currently do it with camera.parent = mesh which works fine.

The API seems a bit inconsistent having getters/setters and public attributes for the same values, yet leaving some of them out in certain classes.

1 Like

Oh, i think i fond the problem.

setParent and addChild are methods of AbstractMesh. But the camera is not an instance of AbstractMesh.
So you can only set the parent of the camera the way you did.

As player.addChild(camera) is trying to call camera.setParent(player) this throws an error as the camera does not have a setParent method.

2 Likes