Rotate on delay

hi everyone,
I have a sript for rotating my camera, but I dont want it to start immedeatly rather like 5 seconds after the application launch. I wish for it to loop the script after 20 seconds how do I do theese 2 things? this is the code I have now:

var Moving = pc.createScript(‘moving’);

// initialize code called once per entity
Moving.prototype.initialize = function() {

};

// update code called every frame
Moving.prototype.update = function(dt) {
this.entity.translateLocal(0, 0, 0);
this.entity.rotateLocal(0.2,0,0);
};

You can keep track of the amount of time has passed in the update function. ie;

// initialize code called once per entity
Moving.prototype.initialize = function () {
    this.secsSinceStart = 0;
};

// update code called every frame
Moving.prototype.update = function (dt) {
    this.secsSinceStart += dt;

    if (this.secsSinceStart >= 5) {
        this.entity.translateLocal(0, 0, 0);
        this.entity.rotateLocal(0.2, 0, 0);
    }
};

Not sure what you mean by this? You want to start again after 20 secs?

1 Like

thanks a lot @yaustar!
a new problem occured… I want the 0.2 rotation to keep going for another 5 seconds and then stop.
then after 5 seconds stop i wish for the script to start over again.

does it make sense?

In which case, you can use the same logic as above to keep track of how long it has been rotating for and stopped for. You will also need to keep track of whether it is ‘stopping’ or ‘rotating’ mode too.

thanks for the swift reply!
however I am not familiar with the vocabulary as to how one stops the rotation?

I have searched far and wide but cannot come up with an answer either to how to reset the script …

You do not reset the script as such, but you simply reset the values that script changes. For example, you have some angle that is somehow changed by the script when your application is running. To reset the script, you simply check for some condition to meet (like some time passes or some event happens) and you reset that angle back to its original value. @yaustar gave you a good example of how to do something after some time passes.

thanks. so there is no loop() function ?

There is. Check the code of @yaustar more closely:

// update code called every frame
Moving.prototype.update = function (dt) {... 

That function runs every frame. Every script in Playcanvas may have this method and it will be run by the engine on every frame. This is your loop.

If you are looking for a tweener, try using this: https://github.com/playcanvas/playcanvas-tween

is there a way to rotate only 90 degrees and not keep on rotating 90 degrees every frame?

This is a simple conditional statement in Javascript:

if (value < 90) {
    // do something
}

I would probably recommend to go through Javascript tutorials and get familiar with the language first.

allright thank you LeXXik! that is for sure the way forward !

I’ve created a simple timer library here that can be used to help with what you are doing above: https://playcanvas.com/project/691984/overview/timers

1 Like