Using tween for camera movement

@smokys, I think tween is not suitable for your use case. It is good to use for situations, when you have some fixed points around your scene that you wish to transition your camera between.

When you have a dynamic object that is contantly moving, then you can interpolate the camera position directly. For example:

/* jshint esversion: 6 */

class Script extends pc.ScriptType {
    
    initialize() {
        // for example some player entity you want to look at
        this.player = this.app.root.findByName('Player');
        
        // as from your example, an entity that is used as camera pivot that it shoudl follow
        this.pivot = this.app.root.findByName('Position_1');

        // well, the camera itself
        this.camera = this.app.root.findByName('Camera');

        // a temporary vector that we can re-use each frame
        this.inter = new pc.Vec3();
    }
    
    update(dt) {
        // find intermediate position for camera, based on where the pivot and camera currently are
        const speed = 0.3; // from 0 to 1, controls how tightly the camera sticks to pivot entity
        const current = this.camera.getPosition();
        const target = this.pivot.getPosition();
        const inter = this.inter.lerp(current, target, 1 - Math.pow(speed, dt));
        
        // set camera position to that intermediate location
        this.camera.setPosition(inter);
        
        // also, make the camera look at the player
        const playerPosition = this.player.getPosition();
        this.camera.lookAt(playerPosition);
    }
    
}

pc.registerScript(Script, 'script');
3 Likes