Control entity update interval

okay I have a code to add jitterness to an entity

``

VehicleJerk.prototype.update = function(dt) {
    setInterval(()=>{
    var plusOrMinus = Math.random() < 0.5 ? -0.005 : 0.005;
    this.entity.setLocalPosition(plusOrMinus + this.entity.getLocalPosition().x, this.entity.getLocalPosition().y, this.entity.getLocalPosition().z);    
}, 5000);

};
``

well this code adds jitter but the update function is so fast that the setInterval does nothing to slow down the process, I simply want to slow down the process and call that function every 5 seconds automatically, the update calls the function too fast, how can I achieve the 5 second interval between each call?

Hi @sir_akc,

Your code can’t work correctly since you start a new interval each frame (and that can destroy performance as well).

Move it to your initialize method, and play with the interval timespan there.

Of course you can drop your interval completely and use only the update method, by calculating the time that has passed yourself and resetting a timer. Try searching for a timer script in the forums, I think there are some around.

1 Like