How do I trigger an event that turns animation on?

I’ve attached the below code to a door that’s intended to slide open vertically when the “openDoorTrigger” event is fired. This does work to open the door, but there is no visible animation of the door sliding since the loop completes before frame updating. How can I change this code so that I can see the door slide open? Thanks in advance for any insight you can share.

var OpenDoor = pc.createScript('openDoor');

var doorShiftAmount = 0.001;
var doorMovementSteps = 300;

// initialize code called once per entity
OpenDoor.prototype.initialize = function() {
    // Listen for the 'openThisDoor' event so when it is fired, call
    // the function openThisDoor event"openDoorTrigger", this.openThisDoor, this);

OpenDoor.prototype.openThisDoor = function() {
    for(var i=1; i<doorMovementSteps; i++)
        {this.entity.translateLocal(0, doorShiftAmount, 0);


You need to put the movement into the update part of the script so one step happens every frame. In addition it’s worth using the actual amount of time the frame took to ensure the movement completes in a sensible period.

var doorShiftTime = 3;
var shiftPerSecond = 3;
OpenDoor.prototype.initialize = function() {"openDoorTrigger", function() { this.opening = true; this.time = 0; }, this);
   this.opening = false;
OpenDoor.prototype.update = function(dt) {
     if(this.opening) {
         this.time += dt;
         if(this.time < doorShiftTime) {
             this.entity.translateLocal(0, shiftPerSecond * dt, 0);

There are a whole host of ways of doing this. For instance using a lerp to move between 0 and the shifted amount, just setting the local.y - this is sort of like your original idea.


Thanks Mike. This is very helpful to me. I’m new to game coding, and was thinking there must be a way to trigger a temporary animation without having a constantly running update loop, but I’m now sensing these constant status-checking loops are standard for games.

There are some fancy techniques to make it “feel” more like you say. For instance you can code a thing that basically does the update loop in a method inside the trigger event. It would still be doing the same thing, and would involve writing an event handler for the framework’s ‘update’ event and then removing it after. I do this often because I prefer the flow of the code to my eye, but that said, it’s a bit more advanced and needs a couple of helper functions to make it look nice. Personally at the start I’d avoid these kind of complexities to keep it clear in your mind what is happening.

Thanks Mike. I appreciate the insights and the suggestion to start simple with the coding format. What you provided previously is a great help for me to move forward on an initial prototype. Thanks again.