[SOLVED] How to lerp between positions

Thats right, I dont know how to lerp. If someone could tell me how to lerp from the three not values to the three yes values that would be great. If I need to change the values to two attribute entities to use their positions thats fine I can do that.

var Aimer = pc.createScript('aimer');

// PLAYER CAMERA
Aimer.attributes.add('playcam', {
type: 'entity',
});

// FOV WHILE AIMING
Aimer.attributes.add('aimfov', {
type: 'number',
});

// SCOPE
Aimer.attributes.add('scope', {
type: 'entity',
});

// MULTIPLIED WITH GLOBALSENS TO GET REAL SENSITIVITY WHILE AIMING
Aimer.attributes.add('aimsens', {
type: 'number',
});

// POSITION NOT AIMING
Aimer.attributes.add('notx', {
type: 'number',
});
Aimer.attributes.add('noty', {
type: 'number',
});
Aimer.attributes.add('notz', {
type: 'number',
});

// POSITION AIMING
Aimer.attributes.add('yesx', {
type: 'number',
});
Aimer.attributes.add('yesy', {
type: 'number',
});
Aimer.attributes.add('yesz', {
type: 'number',
});

Aimer.attributes.add('useaimlocator', {
type: 'boolean',
});

Aimer.prototype.update = function(dt) {

    // THREE DEBUG FUNCTIONS CAN BE REMOVED
    Regfovvar = 60;
    Aimfovvar = this.aimfov;
    Aimsensvar = (Globalsens * this.aimsens)

    // MOVES THIS ENTITY AND CHANGES FOV
    if(this.app.mouse.isPressed(pc.MOUSEBUTTON_RIGHT))
    {
        this.entity.setLocalPosition(this.yesx, this.yesy, this.yesz);
        this.playcam.camera.fov = this.aimfov;
        this.scope.enabled = true;
        Realsens = (Globalsens * this.aimsens)

        // IF THE BOOLEAN WAS SELECTED THIS ENABLES AND DISABLES THE SCOPE
        if(this.useaimlocator === true)
        {
            this.entity.setPosition(Aimlocx, Aimlocy, Aimlocz)
        }
    }

    // SETS ALL ENTITIES TO DEFAULT VALUES IF NOT AIMING
    else
    {
        this.entity.setLocalPosition(this.notx, this.noty, this.notz);
        this.playcam.camera.fov = 60;
        this.scope.enabled = false;
        Realsens = Globalsens
    }
};

Can you elaborate a bit on what you mean by lerping 3 values?

var currentPos = currentPosition (pc.Vec3);
var targetPos = targetPosition (pc.Vec3);

var newPos = new pc.Vec3().lerp(currentPos, targetPos , alpha);

That would work for 2 positions, but it doesn’t work for the third. Alpha is only how fast it tweens.

Oh yes my bad it should be like this

var startPos = startPosition (pc.Vec3); //Not Current position
var targetPos = targetPosition (pc.Vec3);

var newPos = new pc.Vec3().lerp(startPos, targetPos , alpha);

eg:

var startPos = this.entity.getPosition.clone();
var targetPos = this.entity.getPosition.clone().add(new pc.Vec3(0, 10, 0));

 var newPos = new pc.Vec3().lerp(startPos, targetPos , 0.5);

this.entity.setPosition(newPos);

i meant 3 positions as in x y z to another x y z, From a point in space using 3 values to another position in space using 3 values

So testing this I got an error that says “this.entity.getPosition.clone is not a function”, because I believe it should be “this.entity.getPosition().clone”

I fixed that and did some rearranging but the entity just moves instantly. What am I doing wrong?

var Aimer = pc.createScript('aimer');

// POSITION FOR AIMING
Aimer.attributes.add('aimpos', {
type: 'entity',
});

// POSITION FOR NOT AIMING
Aimer.attributes.add('notaimpos', {
type: 'entity',
});

// PLAYER CAMERA
Aimer.attributes.add('playcam', {
type: 'entity',
});

// FOV WHILE AIMING
Aimer.attributes.add('aimfov', {
type: 'number',
});

// SCOPE
Aimer.attributes.add('scope', {
type: 'entity',
});

// MULTIPLIED WITH GLOBALSENS TO GET REAL SENSITIVITY WHILE AIMING
Aimer.attributes.add('aimsens', {
type: 'number',
});

// POSITION NOT AIMING
Aimer.attributes.add('notx', {
type: 'number',
});
Aimer.attributes.add('noty', {
type: 'number',
});
Aimer.attributes.add('notz', {
type: 'number',
});

// POSITION AIMING
Aimer.attributes.add('yesx', {
type: 'number',
});
Aimer.attributes.add('yesy', {
type: 'number',
});
Aimer.attributes.add('yesz', {
type: 'number',
});

Aimer.attributes.add('useaimlocator', {
type: 'boolean',
});

Aimer.prototype.update = function(dt) {

    var startPos = this.notaimpos.getPosition().clone();
    var targetPos = this.aimpos.getPosition().clone(); //.add(new pc.Vec3(0.01, 0, 0));

    // THREE DEBUG FUNCTIONS CAN BE REMOVED
    Regfovvar = 60;
    Aimfovvar = this.aimfov;
    Aimsensvar = (Globalsens * this.aimsens)

    // MOVES THIS ENTITY AND CHANGES FOV
    if(this.app.mouse.isPressed(pc.MOUSEBUTTON_RIGHT))
    {
        var newPos = new pc.Vec3().lerp(startPos, targetPos, 1);
        this.entity.setPosition(newPos);
        this.playcam.camera.fov = this.aimfov;
        this.scope.enabled = true;
        Realsens = (Globalsens * this.aimsens)

        // IF THE BOOLEAN WAS SELECTED THIS ENABLES AND DISABLES THE SCOPE
        if(this.useaimlocator === true)
        {
            this.entity.setPosition(Aimlocx, Aimlocy, Aimlocz)
        }
    }

    // SETS ALL ENTITIES TO DEFAULT VALUES IF NOT AIMING
    else
    {
        var newPos = new pc.Vec3().lerp(targetPos, startPos, 1);
        this.entity.setPosition(newPos);
        this.playcam.camera.fov = 60;
        this.scope.enabled = false;
        Realsens = Globalsens
    }
};

Alright so I just figured out that the third lerp value has to be constantly changed. I fixed it.

1 Like