Tween Happening Instantly

Hello!

I am running the script below and the tween is going to the right spot, but it is happening instantly instead of over two seconds. Can anyone think of why this is happening? I am currently converting a project that was using physics over into hard coded animations via tween and I’m running into this issue.

       var targetPosition = this.entity.getLocalPosition();
        targetPosition.x = targetPosition.x + xComponent;
        targetPosition.z = (targetPosition.z + yComponent)*-1;
        
        if (this.tween) {
            this.tween.stop();
        }
        //tween it into the game
        this.tween = this.entity.tween(this.entity.getLocalPosition())
            .to(targetPosition, 2, pc.Linear)
            .delay(0)
            .loop(false)
            .yoyo(false);
        // start the tween
        this.tween.start();`

Hi @Jake_Johnson,

The pc.Vec3 returned in your first code line should be considered read only. Try cloning it first so current/target positions are unique pc.Vec3 instances:

var targetPosition = this.entity.getLocalPosition().clone();

Wow thank you so much that fixed it. Why did that fix it exactly?

So, the pc.Vec3 return when you call the pc.Entity getPosition() or getLocalPosition() methods is used internally by the engine.

That means even though you may change it to the target position, after the next hierarchy update (e.g. next frame) it will be overridden by the engine.

1 Like