[SOLVED] Door script

I am not good with the console

I don’t know how to do it

You can open the console of your browser with the F12 key. When the line is executed you can see it in the console. This way you can find out what’s happening in your script.

oh ok

it opens inspect

wait i think i figured it out if the distance is less than 5 and the F key is pressed I have set the value to 0 i should be able to do that.

1 Like

here is what I have so far, anything I need to change?

var DoorOpener = pc.createScript('doorOpener');

DoorOpener.attributes.add('playerEntity', {type: 'entity'});
DoorOpener.attributes.add('timeToOpen', {type: 'number', default: 1.0});

// update code called every frame
DoorOpener.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_E)){
        let door_position = this.entity.getPosition();
        let player_position = this.playerEntity.getPosition();
        let distance = door_position.distance(player_position);

        if(distance < 5.0){
            this.is_opening = true;
            this.opening_anim_time = 0.0;
        }

    }

    if (this.is_opening == true){
        if(this.opening_anim_time/this.timeToOpen > 1.0){
            this.is_opening = false;
        }

        let opening_angle = pc.math.lerp(0, -90, this.opening_anim_time / this.timeToOpen);
        this.entity.setLocalEulerAngles(0, opening_angle, 0);
        this.opening_anim_time += dt;
        
    }

    if(this.app.keyboard.isPressed(pc.KEY_F)){
        if(distance < 5.0){
            this.is_opening = true;
            this.opening_anim_time = 0.0;
        }
    }

    if(this.is_opening == true){
        if(this.opening_anim_time/this.timeToOpen > 1.0){
            this.is_opening = false;
        }

        let opening_angle = pc.math.lerp(0, 0, this.opening_anim_time / this.timeToOpen);
        this.entity.setLocalEulerAngles(0, opening_angle, 0);
        this.opening_anim_time +=dt;
    }


};

Right now the value of distance is only updated with the E key. That means when you press the F key the value of distance is not correct anymore.

I am confused, I have been trying for hours but it still wont animate close. it just sets its rotation.

Here is what I have now

var DoorOpener = pc.createScript('doorOpener');

DoorOpener.attributes.add('playerEntity', {type: 'entity'});
DoorOpener.attributes.add('timeToOpen', {type: 'number', default: 1.0});

// update code called every frame
DoorOpener.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_E)){
        let door_position = this.entity.getPosition();
        let player_position = this.playerEntity.getPosition();
        let distance = door_position.distance(player_position);

        if(distance < 5.0){
            this.is_opening = true;
            this.opening_anim_time = 0.0;

            if(distance < 5.0 && this.app.keyboard.isPressed(pc.KEY_F)){
                if(this.entity.getRotation().y < 89) {
                    this.entity.rotateLocal(0,-2,0);
                    else{
                        if(this.entity.getRotation().y < 1){
                            this.entity.setRotation(0,0,0);
                        }
                    }
                }
            }
        }
    }

    if (this.is_opening == true){
        if(this.opening_anim_time/this.timeToOpen > 1.0){
            this.is_opening = false;
        }

        let opening_angle = pc.math.lerp(0, 90, this.opening_anim_time / this.timeToOpen);
        this.entity.setLocalEulerAngles(0, opening_angle, 0);
        this.opening_anim_time += dt;
        
    }
    
};

I don’t know how I can help you further. If you can’t read the code, you can’t make it work.

It’s ok, I will learn soon. Thanks for the help though.

If you share a link of your project I’ll see what I can do when I have some time.

https://playcanvas.com/editor/scene/1437310

here you go

var DoorOpener = pc.createScript('doorOpener');

DoorOpener.attributes.add('playerEntity', {type: 'entity'});
DoorOpener.attributes.add('timeToOpen', {type: 'number', default: 1.0});

// update code called every frame
DoorOpener.prototype.update = function(dt) {
    if (this.app.keyboard.wasPressed(pc.KEY_E)){
        let door_position = this.entity.getPosition();
        let player_position = this.playerEntity.getPosition();
        let distance = door_position.distance(player_position);

        if(distance < 5.0){
            if (this.door_open) {
                this.is_closing = true;
            }
            else {
                this.is_opening = true;
            }

            this.opening_anim_time = 0.0;
        }
    }

    if (this.is_opening || this.is_closing){
        if(this.opening_anim_time/this.timeToOpen > 1.0){
            if (this.is_opening) {
                this.door_open = true;
                this.is_opening = false;
            } 
            else if (this.is_closing) {
                this.door_open = false;
                this.is_closing = false;
            }
        }

        if (this.is_opening) {
            let opening_angle = pc.math.lerp(0, 90, this.opening_anim_time / this.timeToOpen);
            this.entity.setLocalEulerAngles(0, opening_angle, 0);
            this.opening_anim_time += dt;
        }
        else if (this.is_closing) {
            let opening_angle = pc.math.lerp(90, 0, this.opening_anim_time / this.timeToOpen);
            this.entity.setLocalEulerAngles(0, opening_angle, 0);
            this.opening_anim_time += dt;
        }
    }
};

Works great, Thanks!

1 Like