(Solved) Speed not increasing by acceleration

So I tried to give acceleration to the Speed variable, but the variable is only given the value of ac, and its not getting any higher/lower than that.

and my character is not moving… no scratch that, i accidentally set that frame as a tween. The speed is not getting larger or smaller

var Speed = 0
var mSpeed = 10
var ac = 1

//Speed control for basic movement
if (isKeyDown('a')){
    if (Speed !== -mSpeed){
    Speed -= ac;
    } else if (Speed <= -mSpeed){
        Speed = -mSpeed;
    }
} else if (isKeyDown('d')){
    if (Speed !== mSpeed){
    Speed += ac;
    } else if (Speed >= mSpeed){
        Speed = mSpeed;
    }
} else if (Speed !== 0 || isKeyDown('a'&&'d')){
    if (Speed > 0){
        Speed -= ac
    } else if (Speed < 0){
        Speed += ac
    } else {
        Speed = 0
    }
    
}

this.x += Speed;

project.Speed_txt.setText(Speed)

That’s because you defined Speed, mSpeed, and ac as 0,10,1, and therefore, these numbers will always be defined as 0,10,1 since it’s in the update script. Here’s a solution:

Solution

Make the variable connected to the object

default script
this.Speed = 0;
this.mSpeed = 10;
this.ac = 1;
update script
//Speed control for basic movement
if (isKeyDown('a')){
    if (this.Speed !== -this.mSpeed){
    this.Speed -= this.ac;
    } else if (this.Speed <= -this.mSpeed){
        this.Speed = -this.mSpeed;
    }
} else if (isKeyDown('d')){
    if (this.Speed !== this.mSpeed){
    this.Speed += this.ac;
    } else if (this.Speed >= this.mSpeed){
        this.Speed = this.mSpeed;
    }
} else if (this.Speed !== 0 || isKeyDown('a'&&'d')){
    if (this.Speed > 0){
        this.Speed -= this.ac
    } else if (this.Speed < 0){
        this.Speed += this.ac
    } else {
        this.Speed = 0
    }
    
}

this.x += this.Speed;

Speed_txt.setText(this.Speed);
1 Like