[SOLVED] I want a entity to rotate a certain amount than stop, but it is not stopping at the end rotation

I have a script that I want to rotate up until it hits 90 degrees on keypress, and go down until -12 degrees on keypress (stopping when it reaches those set degrees.) I have the script all set, but it doesn’t want to stop at the certain point. Here is the script without anything from stopping it, I just need it to stop moving after reaching (90 and -12 degrees).

var PivotWs = pc.createScript('pivotWs');
PivotWs.prototype.initialize = function() {
    this.pivotAtX = 0;
};
PivotWs.prototype.update = function(dt) {
    this.entity.rotateLocal(this.pivotAtX, 0, 0);
    if(this.app.keyboard.isPressed(pc.KEY_W)){
        this.pivotAtX=0.5;
    }
    if(this.app.keyboard.isPressed(pc.KEY_S)){
        this.pivotAtX=-0.5;
    }
    if(!this.app.keyboard.isPressed(pc.KEY_W) && !this.app.keyboard.isPressed(pc.KEY_S)){
        this.pivotAtX=0;
    }
};

@Kyle_3_1415 Please look into the method called clamp. Clamp allows you to limit a value between a min and a max value. Here are some forum posts that address using clamp to control a variety of different variables.

https://forum.playcanvas.com/search?q=clamp

Here is a link to the API documentation.

Thank you, it works now :grin: ! here is the working code for future reference :

var PivotWs = pc.createScript('pivotWs');
PivotWs.prototype.initialize = function() {
    this.pivotAtX = 0;
};
PivotWs.prototype.update = function() {
    let angle = this.pivotAtX;
    angle = pc.math.clamp(angle, -14, 90);
    this.entity.setLocalEulerAngles(angle, 0, 0);
    if(this.app.keyboard.isPressed(pc.KEY_W)){
        this.pivotAtX+=0.5;
    }
    if(this.app.keyboard.isPressed(pc.KEY_S)){
        this.pivotAtX-=0.5;
    }
    if(!this.app.keyboard.isPressed(pc.KEY_W) && !this.app.keyboard.isPressed(pc.KEY_S)){
        this.pivotAtX=this.pivotAtX;
    }
};

@Kyle_3_1415 Nice and thanks for sharing your code so others can have a look as well.