Why getTarget() dosn't give me the value set buy setTarget()

Hello,
I use this simple code :

camera1 =  new BABYLON.ArcRotateCamera("Camera",0, 0, 10, new BABYLON.Vector3.Zero(), scene);
scene.activeCamera=camera1;
camera1.attachControl(canvas,false);
var vv=new BABYLON.Vector3(0,1,1);
camera1.setTarget(vv);
console.log(camera1.getTarget());

why it consoles the old target (0,0,0) ??

thanks

1 Like

Hey!
This is because all internal properties are only updated at render time (to improve performance)
You can manually force it to update with camera.getViewMatrix(true):
https://www.babylonjs-playground.com/#YNU38U

Hi again, Anes! I think you found a bug.

The arcRotateCamera is a descendant of targetCamera.

Let’s go code-sniffin’:

The Arc cam has ITS OWN setTarget(), but it uses targetCam for its getTarget(). Here’s arcCam’s setTarget():

https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/arcRotateCamera.ts#L948

Notice that it never sets this._currentTarget property (which lives on targetCam).

Now let’s look at targetCam getTarget (which arcCam uses for itself, too):

https://github.com/BabylonJS/Babylon.js/blob/master/src/Cameras/targetCamera.ts#L270

Notice that getTarget() returns this._currentTarget… a property that was NOT set by arcCam setTarget().

BUT, arcCam._currentTarget IS set… in line 1020, from within a private func called _getViewMatrix(). So, maybe, arcCams once called this._getViewMatrix(), but quit doing it, for some reason. Not sure, yet.

In this playground, I force-set camera1._currentTarget = vv; in line 9, and line 10 is now working correctly. I think we need to add a line or two… to arcCam’s setTarget() code.

Nice find, thx for the report!

1 Like

@Deltakosh yes I verify this by creating a button and when scene is rendred I click to console the getTarget() and it works but how about universal camera I tired to change the target and then console it but the value is random ! I just change the camera type

camera =  new BABYLON.UniversalCamera("Camera",new BABYLON.Vector3(0,10,0), scene);

https://www.babylonjs-playground.com/#YNU38U#1

1 Like

This is because FreeCamera and UniversalCamera work by targeting a position.

When you call setTarget, we compute a new one which is far enough so you can then move it with the mouse

@Deltakosh so if I understand the traget in universal camera and in arcCamera are not the same, in arcCam the target is where the camera looking to but in universal camera the target is where the camera can go by moving it , doesn’t it ?

@Deltakosh the proble is even if I use UniversalCamera when I change the target I can see it in the PG that where camera is looking through is changing to and if I make (0,0,0) the camera looks to origine and if I make (0,0,10) the camera looks to that point, but why when I console it it gives me another point !!

Targets for both are where the camera looks but for FreeCamera based (including UniversalCamera) the target is on a line starting on the camera and going through the target you gave. This is because with these cameras, when you move the mouse you will control the target.

@Deltakosh understand, and why if I didn’t move the mouse, why getTarget() doesn’t give me the target that I gave ?

Because we move it immediately where it will be useful: