Camera.setTarget() with world coordinates


currently Camera.setTarget() uses local coordinates for it’s calculations. If you set a parent to the camera, this function only makes sense in the camera’s local space, but not in world space anymore.

I’d like to propose a function Camera.setTargetWorld() or similar, which works with target vectors in world coordinates.


Sounds like a wonderful suggestion!
Do you want to try and tackle this yourself, or is it just a feature request from the team? (Both are fine, BTW, I am just wondering :slight_smile: )

I tried to implement this earlier and realized, it’s just a mattter of transforming the target by the cameras global position, which already is computed and exposed. Something like:

const worldTarget = new Vector3(...);
const localTarget = worldTarget.subtract(camera.globalPosition);

I don’t think, there is a need for a new function in the framework here. But a remark somewhere in the documentation might be nice. setTarget() not behaving as I expected, took me really off guard and way more time than I’d like to admit. :sweat_smile:

1 Like

Tou can make a PR with a new parameter to define the space if you want ?

1 Like

I thought about an optional parameter, but then everybody has to live with the extra if.

Maybe just a remark in the jsDoc like:

  * Defines the target the camera should look at.
  * @param target Defines the new target as a Vector relative to the camera position
public setTarget(target: Vector3): void {

Not sure about the wording though. ‘Camera local space’ would be inaccurate, since rotation is ignored. It’s just relative to the camera’s position.

Maybe camera's local position instead of space?
Having said that - I believe it is enough to add a switch with the space selected (if you want to avoid the if), and still allow the user to select the space in which they are setting the target.