How to smoothen the movement on trigger enter with lerp?

I have this code

moveCarUp(pos, entity){
        pos.y += 0.2; 
        entity.setLocalPosition(pos);
    }

but how to smoothen the 0.2 so it should be not sharp move to up but smoothly like some exponential with lerp? I mean not 0.2 but sth like 0.01, 0.02, …, 0.2
why I want this: because I want the car to moves up a little to follow the bump

Not sure if I understood correctly, since your example [0.01, 0.02, …, 0.2] shows a linear increase by 0.01.

If you want to start the movement quickly and gradually slow it down, you can use an easing function directly:

Script.prototype.initialize = function() {
    this.y = 0;
    this.time = 0;
};

Script.prototype.cubicOut = function(k) {
    return --k * k * k + 1;
};

Script.prototype.update = function(dt) {
    if (this.y < 1) {
        this.time += dt;
        this.y = this.cubicOut(0.5 * this.time);
        this.entity.setPosition(0, this.y, 0);
    }
};

If you don’t need to do any additional calculations, while the car is in the air, you might want to consider using Tween library. Otherwise, you can find its easing functions on github. For example, I copied the .cubicOut() directly from there.

And if you are using Ammo physics, then you don’t need to calculate these bumps yourself. Instead, you would want to send an impulse to the car from the bottom, which will bump it up and the physics engine will handle the rest.

2 Likes