How to adding Lerping in copy rotation of other entity?

Hi everybody! I am looking for the best way to add lerping to a entity that copy the rotation of other with a fast movement

I am using this code now for copy rotation


var CopyRotation = pc.createScript('copyRotation');

CopyRotation.attributes.add('sourceEntity', {type: 'entity'});

// update code called every frame
CopyRotation.prototype.update = function(dt) {
    this.entity.setRotation(this.sourceEntity.getRotation());
};

Hi @Luis_Mb,

What you are looking for is slerp(), that is spherical interpolation between two quaternions:

https://developer.playcanvas.com/api/pc.Quat.html#slerp

getRotation() returns a quaternion and setRotation() expects one.

1 Like
const quat = new pc.Quat();

update(dt) {
    const current = entity.getRotation();
    const target = target.getRotation();

    // make 0.01 smaller or multiply dt by scalar (> 1.0) for 
    // faster convergence
    quat.slerp(current, target, 1 - Math.pow(0.01, dt);

    entity.setRotation(quat)
}

2 Likes

hello, what iā€™m trying to do is to give a recoil effect to the camera usin slerp. I declared the the targetRotation and currentRotation like this

this.targetRotation = new pc.Quat();
this.currentRotation = new pc.Quat();

Should i have declare them like you did or like this inside the init like this

 this.currentRotation = this.currentRotation.getRotation();
 this.targetRotation= this. targetRotation.getRotation();