[SOLVED] How To Make Camera Movement Smooth

Hi @GmeMkr,

Here is a way to smooth out your camera when following the ball:

var CameraScript = pc.createScript('cameraScript');

//Attributes
CameraScript.attributes.add("player", { type: 'entity', title: 'Player' });

// initialize code called once per entity
CameraScript.prototype.initialize = function() {

    this.pivotPoint = new pc.Vec3();
    
    // --- the smoothness of the camera, larger values make the movement smoother
    this.inertia = 0.15;

    // --- the initial offset of the camera position set in editor
    this.offset = this.entity.getLocalPosition().clone();
};

// update code called every frame
CameraScript.prototype.update = function(dt) {
      
    var inertiaFactor = Math.min(dt / this.inertia, 1);
    var pos = this.player.getPosition();

    // --- here is where we smooth out the current position based on the inertia factor
    this.pivotPoint.lerp(this.pivotPoint, pos, inertiaFactor);

    this.entity.setPosition(this.pivotPoint);
    this.entity.translateLocal(this.offset);
};
4 Likes