[SOLVED] I have a problem with enabling entity

i have a problem where I have to simultaneously hold 2 and M to open the menu(similar to the inventory) and I have it set to M

my entire update function:

// HERE START YOUR UPDATE FUNCTION // update code called every frame
Movement.prototype.update = function(dt) {
    
    // Switch weapon
 if (this.app.keyboard.isPressed(pc.KEY_1) || this.app.keyboard.isPressed(pc.KEY_Q)) {
        this.app.root.findByName('Flashlight').enabled = true;
        this.app.root.findByName('Pistol').enabled = false;
    }this.app.root.findByName('Menu').enabled = false;
    
 if (this.app.keyboard.isPressed(pc.KEY_2) || this.app.keyboard.isPressed(pc.KEY_E)) {
        this.app.root.findByName('Flashlight').enabled = false;
        this.app.root.findByName('Pistol').enabled = true;
        this.app.root.findByName('Menu').enabled = false;
     
 if (this.app.keyboard.isPressed(pc.KEY_M)) {
        this.app.root.findByName('Flashlight').enabled = false;
        this.app.root.findByName('Pistol').enabled = false;
        this.app.root.findByName('Menu').enabled = true;
      }
 }

Would you like to explain a little better what your question or problem is?

to bring out the enu, i have to press 2 and m at the same time he i have it set to just ‘M’

Do you mean something like this?

if (this.app.keyboard.isPressed(pc.KEY_M) && this.app.keyboard.isPressed(pc.KEY_2)) {
    this.app.root.findByName('Flashlight').enabled = false;
    this.app.root.findByName('Pistol').enabled = false;
    this.app.root.findByName('Menu').enabled = true;
}

yes, but I don’t want to press them both… i want it to only press M and it opens the menu.

But as far as I can see, your own code is already working like that?

but when you play, it makes you press 2 and M
here’s the editor(i would Bookmark this) lol
https://playcanvas.com/editor/scene/1099897

You are setting the “enabled” property of your menu entity to false on every update. I would also keep a reference of your Menu Entity instead of calling findByName every update.

2 Likes

What @Sinlyu said is correct. Not all code is inside the parentheses of the if statements. In the code below I fixed this error and optimized your script as well. For that you have to add the first part in your initialize function and you have to replace the code of your update function with the second part.

Initialize:

this.flashlight = this.app.root.findByName('Flashlight');
this.pistol = this.app.root.findByName('Pistol');
this.menu = this.app.root.findByName('Menu');

Update:

if (this.app.keyboard.isPressed(pc.KEY_1) || this.app.keyboard.isPressed(pc.KEY_Q)) {
    this.flashlight.enabled = true;
    this.pistol.enabled = false;
    this.menu.enabled = false;
}
    
if (this.app.keyboard.isPressed(pc.KEY_2) || this.app.keyboard.isPressed(pc.KEY_E)) {
    this.flashlight.enabled = false;
    this.pistol.enabled = true;
    this.menu.enabled = false;
}
     
if (this.app.keyboard.isPressed(pc.KEY_M)) {
    this.flashlight.enabled = false;
    this.pistol.enabled = false;
    this.menu.enabled = true;
}
2 Likes

okah

i added it, and i came up with this error


my code:

var Movement = pc.createScript('Movement');

Movement.attributes.add('camera', {
    type: 'entity',
    description: 'Optional, assign a camera entity, otherwise one is created'
});

Movement.attributes.add('power', {
    type: 'number',
    default: 2500,
    description: 'Adjusts the speed of player movement'
});

Movement.attributes.add('lookSpeed', {
    type: 'number',
    default: 0.25,
    description: 'Adjusts the sensitivity of looking'
});

// initialize code called once per entity
this.flashlight = this.app.root.findByName('Flashlight');
this.pistol = this.app.root.findByName('Pistol');
this.menu = this.app.root.findByName('Menu');

Movement.prototype.initialize = function() {
    this.force = new pc.Vec3();
    this.eulers = new pc.Vec3();

    var app = this.app;

    // Listen for mouse move events
    app.mouse.on("mousemove", this._onMouseMove, this);

    // when the mouse is clicked hide the cursor
    app.mouse.on("mousedown", function () {
        app.mouse.enablePointerLock();
    }, this);

    // Check for required components
    if (!this.entity.collision) {
        console.error("First Person Movement script needs to have a 'collision' component");
    }

    if (!this.entity.rigidbody || this.entity.rigidbody.type !== pc.BODYTYPE_DYNAMIC) {
        console.error("First Person Movement script needs to have a DYNAMIC 'rigidbody' component");
    }
    
    if (this.app.root.findByName('Handgun')){
    this.rounds = 12;
}

    if (this.app.mouse) {
        this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
        this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
        this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
    }
};

// HERE START YOUR UPDATE FUNCTION // update code called every frame
Movement.prototype.update = function(dt) {
    
    // Switch weapon
if (this.app.keyboard.isPressed(pc.KEY_1) || this.app.keyboard.isPressed(pc.KEY_Q)) {
    this.flashlight.enabled = true;
    this.pistol.enabled = false;
    this.menu.enabled = false;
}
    
if (this.app.keyboard.isPressed(pc.KEY_2) || this.app.keyboard.isPressed(pc.KEY_E)) {
    this.flashlight.enabled = false;
    this.pistol.enabled = true;
    this.menu.enabled = false;
}
     
if (this.app.keyboard.isPressed(pc.KEY_M)) {
    this.flashlight.enabled = false;
    this.pistol.enabled = false;
    this.menu.enabled = true;
}
    // If a camera isn't assigned from the Editor, create one
    if (!this.camera) {
        this._createCamera();
    }

    var force = this.force;
    var app = this.app;

    // Get camera directions to determine movement directions
    var forward = this.camera.forward;
    var right = this.camera.right;

    // movement
    var x = 0;
    var z = 0;

    // Use W-A-S-D keys to move players
    // Check for key presses
    if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.KEY_LEFT)) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D) || app.keyboard.isPressed(pc.KEY_RIGHT)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W) || app.keyboard.isPressed(pc.KEY_UP)) {
        x += forward.x;
        z += forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_S) || app.keyboard.isPressed(pc.KEY_DOWN)) {
        x -= forward.x;
        z -= forward.z;
    }

    // use direction from keypresses to apply a force to the character
    if (x !== 0 && z !== 0) {
        force.set(x, 0, z).normalize().scale(this.power);
        this.entity.rigidbody.applyForce(force);
    }

    // update camera angle from mouse events
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);
    
   setTimeout(function() {
    // Reload
    if (app.keyboard.isPressed(pc.KEY_R)) {
        this.rounds = 12;
    }
}.bind(this), 3000);
    // reload gun magazine
};

Movement.prototype._onMouseMove = function (e) {
    // If pointer is disabled
    // If the left mouse button is down update the camera from mouse movement
    if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;
    }
};

Movement.prototype._createCamera = function () {
    // If user hasn't assigned a camera, create a new one
    this.camera = new pc.Entity();
    this.camera.setName("First Person Camera");
    this.camera.addComponent("camera");
    this.entity.addChild(this.camera);
    this.camera.translateLocal(0, 0.5, 0);
};

// Function start (recognizable by the script name)
Movement.prototype.onMouseDown = function(event) {
    if (this.app.root.findByName('Pistol').enabled === true) {
       // shooting code 
        if (event.button === pc.MOUSEBUTTON_LEFT) {
            // shoot
            if (this.rounds !== 0) {
                var bullet = this.app.root.findByName('Bullet').clone();
                this.app.root.addChild(bullet);
                bullet.reparent(this.app.root);
                bullet.setPosition(this.app.root.findByName('Bullet').getPosition());
                bullet.setRotation(this.app.root.findByName('Bullet').getRotation());
                bullet.enabled = true;
                console.log(bullet);
                // update magazine
                this.rounds = this.rounds - 1;
            }
        }
    }
// Function end
};

i switched them, it works

Good that you solved it yourself this time! You should pay more attention to where you put your code.