[SOLVED] Custom entity property change his value

Hi,
I have a strange behaviour.

The idea is basically the following.
I have a camera, with a target.

I have some hotspots.
These hotspots have the following associated:
A new camera position (entity)
A new target of camera (entity)
It also has other associated elements that do not matter for this case.

When I click on a hotspots this happens:

1 - I keep the position values ​​of the camera and the target

this.originalCameraPosition = this.entity.getLocalPosition();
this.originalTargetPosition = this.focusEntity.getPosition();

2 - I recover the entities of the hotspots for the new camera position and the new target.

var dataSrc = entity.script.pulse;
var target = dataSrc.cameraTarget;
var camera = dataSrc.cameraPosition;

3 - I call the method .move with the values ​​I want the camera to go to

this.move(camera.getLocalPosition(), target.getLocalPosition(), pc.CubicInOut, pc.CubicOut);

If I click again on the hotspot I simply call the .move method so that it returns to the values ​​that I saved initially. that is, this.originalCameraPosition and this.originalTargetPosition


This is the logic, below is the complete code.

The fact is that if I make a console.log, the values ​​of this.originalCameraPosition in the .update method, these values ​​change in each frame and take the values ​​that the camera is taking as it moves with the tween ()
This seems very strange to me, since it is a simple public property that I add to the object as a storage.

Instead, it does not happen with this.originalCameraPosition

This is the hotspot click event handle:

    this.app.on( 'camera:move', function(entity) {
        console.log('camera:move');
        var that = this;
       
        // Move camera and target position to original position
        if (this.isInHotspot) {
            this.move(this.originalCameraPosition, this.originalTargetPosition, pc.CubicInOut, pc.CubicOut, function(){
                that.isInHotspot = false;
            });
        }
        // Move camera and target position to hotspot data positions
        else {
            // Save original target and camera position
            this.originalCameraPosition = this.entity.getLocalPosition();
            this.originalTargetPosition = this.focusEntity.getPosition();
            
            // Get pulse data 
            var dataSrc = entity.script.pulse; // Data from hotspot entity
            var portes = dataSrc.entityObject; // Entity with animation (entity)
            var animation = dataSrc.entityAnimation; // The animation name (string)
            var target = dataSrc.cameraTarget; // The new target for this hotspots
            var camera = dataSrc.cameraPosition; // The new camera position for this hotspot
            
            this.isInHotspot = true;

            // Launch pulse data animation
            portes.animation.play(animation, 1);
            
            // Init tween position movement
            this.move(camera.getLocalPosition(), target.getLocalPosition(), pc.CubicInOut, pc.CubicOut);
        }

    }, this );

And the move method is this:

OrbitCamera.prototype.move = function(cameraPosition, targetPosition, interpolationCamera, interpolationTarget, callback) {
    
    if(this.tweenPosition) {this.tweenPosition.stop(); }
    if(this.tweenTarget) {this.tweenTarget.stop(); }

    this.tweenPosition = this.entity.tween(this.entity.getLocalPosition()).to(cameraPosition, 2, interpolationCamera);

    // lookAt each update
    this.tweenPosition.on('update', function () {
        this.entity.lookAt(this.focusEntity.getLocalPosition());
    }, this);
    
    this.tweenPosition.on('complete', function () {
        if (callback) {
            callback();
        }
    }, this);

    //tween target
    this.tweenTarget = this.focusEntity.tween(this.focusEntity.getLocalPosition()).to(targetPosition, 2, interpolationTarget);

    //start tweens
    this.tweenPosition.start();
    this.tweenTarget.start();  
};

Anywhere know why this value change in update every frame?.

Thanks in advance and sorry for my english.
u.

Looks like you set it every time you are not in a hotspot?

if (this.isInHotspot) {
            this.move(this.originalCameraPosition, this.originalTargetPosition, pc.CubicInOut, pc.CubicOut, function(){
                that.isInHotspot = false;
            });
        }
        // Move camera and target position to hotspot data positions
        else {
            // Save original target and camera position
            this.originalCameraPosition = this.entity.getLocalPosition();
            this.originalTargetPosition = this.focusEntity.getPosition();

(see last two lines)

It’s more difficult to debug without a project to look at?

Hi @yaustar,
No, i check it. this.isInHotspot is just a property to check the state(in, out) of hotspots.

This propertis only has one time the value.

I rewrite the post to a better description.

Thanks!

Ah, I see now.

this.originalCameraPosition = this.entity.getLocalPosition();

I’ve been working in Unity so I forgot about this. As pc.Vec3 is an object, this.originalCameraPosition is now storing a reference, not value, to the local position of the entity hence why it is changing during the tween.

You want to either copy the values or clone the pc.Vec3.

Either:

1:

In init of the script:

this.originalCameraPosition = new pc.Vec3();

And later in the move callback:

this.originalCameraPosition.copy(this.entity.getLocalPosition());

Or 2:
In the move callback:

this.originalCameraPosition = this.entity.getLocalPosition().clone();

Hi @yaustar
Sounds good, I try this solution later.
I’ll keep you informed

Thanks!
David

Hi @yaustar
Work perfectly now (:

thank you so much!