I need to make this script slowly move faster and slower

I have a game that I’m making, and I need help making it move faster and slower smoothly.

var SwitchingPistons = pc.createScript('switchingPistons');
SwitchingPistons.attributes.add('piston1', {
    type: 'entity',
    description: 'first piston'
});
SwitchingPistons.attributes.add('piston2', {
    type: 'entity',
    description: 'second piston'
});
SwitchingPistons.attributes.add('lever', {
    type: 'entity',
    description: 'lever down'
});
SwitchingPistons.attributes.add('levertrue', {
    type: 'entity',
    description: 'lever up'
});
SwitchingPistons.prototype.initialize = function() {
    this.anyNumber = 1;
    this.piston1speed = 1000;
    this.piston2speed = -1000;
};
SwitchingPistons.prototype.update = function() {
    this.piston1.setPosition(new pc.Vec3(32.65, Math.sin(Date.now() / this.piston1speed)+3.55, -39.35));
    this.piston2.setPosition(new pc.Vec3(32.65, Math.sin(Date.now() / this.piston2speed)+3.55, -41.92));
    if(this.app.keyboard.wasPressed(pc.KEY_Q)){
            this.anyNumber += 1;
    }
    if(this.anyNumber == 1){
        this.levertrue.enabled = false;
        this.lever.enabled = true;
        this.piston1speed = 1000;
        this.piston2speed = -1000;
    }
    if(this.anyNumber == 2){
        this.levertrue.enabled = true;
        this.lever.enabled = false;
        this.piston1speed = 100;
        this.piston2speed = -100;
    }
    if(this.anyNumber == 3){
        this.anyNumber = 1;
    }
};

Here is my game

I guess this is a hard one to answer.

Hi @Kyle_3_1415 ,

The forum is certainly happy to help, but it’s difficult without specific questions. You should consider these questions to get better responses:

What behavior am I trying to code?
What code have I tried in order to implement this behavior? (Include this code for others to help)
What behavior is my project exhibiting now?
What (if any) errors is my project generating?
How might others get to my project?
Why do I think the code I have used is not working? (Mostly optional, but can help with troubleshooting on your own)

If you break down each issue you have into chucks with these questions it will make things a lot more simple for both you, and anyone attempting to assist.

I hope this is helpful.

3 Likes

Hi friend . I used AI to maybe help you out with this … To make your game move faster and slower smoothly, you can change the speed of the pistons gradually over time instead of instantaneously. One way to do this is to introduce a variable that represents the current speed of the pistons and use it in the setPosition method instead of the fixed piston1speed and piston2speed values.

Here is an updated version of your code with this approach: In this code, the currentSpeed variable is gradually adjusted towards the targetSpeed variable in each update, using a simple linear interpolation with a factor of 0.1. The dt parameter passed to the update function represents the time elapsed since the last update, and could be used to make the interpolation more precise.

Note that the code also includes some additional logic to prevent the anyNumber variable from going beyond 3, and to simplify the logic for toggling the lever entities based on the anyNumber value.

var SwitchingPistons = pc.createScript('switchingPistons');

SwitchingPistons.attributes.add('piston1', {
    type: 'entity',
    description: 'first piston'
});

SwitchingPistons.attributes.add('piston2', {
    type: 'entity',
    description: 'second piston'
});

SwitchingPistons.attributes.add('lever', {
    type: 'entity',
    description: 'lever down'
});

SwitchingPistons.attributes.add('levertrue', {
    type: 'entity',
    description: 'lever up'
});

SwitchingPistons.prototype.initialize = function() {
    this.anyNumber = 1;
    this.currentSpeed = 1000;
    this.targetSpeed = 1000;
};

SwitchingPistons.prototype.update = function(dt) {
    var t = Date.now() / 1000; // convert time to seconds
    this.currentSpeed += (this.targetSpeed - this.currentSpeed) * 0.1; // gradually change speed
    this.piston1.setPosition(new pc.Vec3(32.65, Math.sin(t * this.currentSpeed) + 3.55, -39.35));
    this.piston2.setPosition(new pc.Vec3(32.65, Math.sin(t * -this.currentSpeed) + 3.55, -41.92));
    if(this.app.keyboard.wasPressed(pc.KEY_Q)) {
        this.anyNumber += 1;
        if (this.anyNumber > 3) {
            this.anyNumber = 1;
        }
        if (this.anyNumber == 1) {
            this.targetSpeed = 1000;
        } else {
            this.targetSpeed = 100;
        }
    }
    if (this.anyNumber == 1) {
        this.levertrue.enabled = false;
        this.lever.enabled = true;
    } else {
        this.levertrue.enabled = true;
        this.lever.enabled = false;
    }
};
2 Likes