[SOLVED] Rotate object after a delay

Hey guys, im trying to make a delay for my objects, its 5 of them and for each i want different delay i tried to make it through attributes add so i would set different delay for each object in inspector

image

but that didnt help, the delay was working but object act totally different as my code says.

so i tried to test it with neww variable

 this.timer = 0;
 this.waitTime = 2;

image

and that worked perfecty with 2 secs delay, but i want to know how to set different delay for each object

these objects i need with delay so they aare not hanging all the same and want them hanging in order

Hi @smokys,

Check the following post it contains helpful advice on the subject and @yaustar shared a timer example project at the end:

2 Likes

sounds great doe, i’d need a little bit of explanation from @yaustar , so timer extension.js should be just added in the project

this line should be added in the initialize function, also moveToRandomPosition in this case should be called this.rotateObjectOnDelay (for example).

this._timerHandle = pc.timer.add(this.durationSecs, this.moveToRandomPosition, this);

so it is:

this._timerHandle = pc.timer.add(this.durationSecs, this.rotateObjectOnDelay, this);

i ill create function:

Trap01Ironball.prototype.rotateObjectOnDelay = function() {

this.entity.rotateLocal(0, this.speed*dt,0);

}

it will rotate object after the delay (this.durationSecs).

but do i need to add the function in update function so it is rotating every frame?

also i dont understand why this line is addded again in rotateObjectOnDelay

this._timerHandle = pc.timer.add(this.durationSecs, this.rotateObjectOnDelay, this);

All the timer does is call a function after X secs. In this case, you would change the state of the script to be rotating.

so i can do it this way: i create new boolean which is set to false in initialize, and is set to true in my new function, and update function is checking when it become true, when the update check it is true then rotate object

also @yaustar, do i need this line again be called in my new function?

this._timerHandle = pc.timer.add(this.durationSecs, this.rotateObjectOnDelay, this);

i want my function be called just once at the beggining when time past

i mean: my new function is going to be like this

Trap01Ironball.prototype.rotateObjectOnDelay = function() {

    this.startRotate = true;

};

and in my update function are these lines:

if(this.startRotate){ 
    
         
     this.applyForce = 69.8;
     this.increaseSpeed = 65;
     this.entity.rotateLocal(0, this.speed*dt,0);
         
     }

would that work ?

Yes the delay works, but my balls are acting totally different with the any kind of delay i set. im clueless, there must be something wrong with my code

image

Here is fix for your code…

:arrow_forward: ironBallTriggerenter.js

var IronBallTriggerenter = pc.createScript('ironBallTriggerenter');

// initialize code called once per entity
IronBallTriggerenter.prototype.initialize = function() {
    this.entity.collision.on("triggerenter", this.onTriggerEnter, this);
    
};

// update code called every frame
IronBallTriggerenter.prototype.update = function(dt) {
    
};

IronBallTriggerenter.prototype.onTriggerEnter = function(entity){
    
    if(entity.tags.has("ironBall"))
    {
        console.log("Hit the trigger", entity.parent.parent.name);
        /*
        this.entities = this.app.root.findByTag('ironBalljoint');
        for (var i = 0; i < this.entities.length; i++)
        {
            this.entity1 = this.entities[i];
            this.trap = this.entity1.script.trap01Ironball;

            this.trap.slowDown = true;
            this.trap.forward = !this.trap.forward;
            this.trap.reverse = !this.trap.reverse;
        }
        */
        
        entity.parent.parent.script.trap01Ironball.slowDown = true;
        entity.parent.parent.script.trap01Ironball.forward = !entity.parent.parent.script.trap01Ironball.forward;
        entity.parent.parent.script.trap01Ironball.reverse = !entity.parent.parent.script.trap01Ironball.reverse;
    }
    
};

// swap method called for script hot-reloading
// inherit your script state here
// IronBallTriggerenter.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/

:arrow_forward: trap01Ironball.js

var Trap01Ironball = pc.createScript('trap01Ironball');

Trap01Ironball.attributes.add("delay", {
   type: 'number',
    default: 2,
    title: 'Starting delay: ',
    description: 'Oneskorenie medzi objektmi'
    
});

// initialize code called once per entity
Trap01Ironball.prototype.initialize = function() {
    
    this.forward = true;
    this.timer = 0;
    this.speed = 70;
    this.slowDown = true;
    this.reverse = false;
    this.startRotate = false;
    this.applyForce = 0;
    this.increaseSpeed = 0;
    this.waitTime = 2;
};

// update code called every frame
Trap01Ironball.prototype.update = function(dt) {
    
    if(this.slowDown){
        if(this.forward){
            if(this.speed >= 0 && this.speed <= 10){
                this.speed += this.applyForce;
        }
        this.speed += this.increaseSpeed*dt;
    }
        
    if(this.reverse){
        if(this.speed  <= 0 && this.speed >= -10){
            this.speed += -this.applyForce;
            }
            this.speed -= this.increaseSpeed*dt;
        }
    }
    
    this.timer += dt;
    
    if(this.timer >= this.delay && this.startRotate === false)
    {
        // if(this.timer >= this.waitTime){

            this.startRotate = true;
            this.applyForce = 69.8;
            this.increaseSpeed = 65;
        // }
        
    }
    
    
    if(this.startRotate){ 

        this.entity.rotateLocal(0, this.speed*dt,0);

    }
    
};

// swap method called for script hot-reloading
// inherit your script state here
// Trap01Ironball.prototype.swap = function(old) { };

// to learn more about script anatomy, please read:
// http://developer.playcanvas.com/en/user-manual/scripting/
1 Like

Yes thank you so much, one minute earlier i found that everything what i was doing was completely wrong lol, but thank you for being faster than me :))

1 Like