[SOLVED] Toggle flash light on and off

Hey! I need help to making a script for toggling the flashlight

var Flashlightcontroller = pc.createScript('flashlightcontroller');


Flashlightcontroller.prototype.initialize = function() {
  this.entity.root.findByName('Player light').enabled = false;
  
};

Flashlightcontroller.prototype.update = function(dt) {
  if (this.app.keyboard.isPressed(pc.KEY_F)){
      this.entity.root.findByName('Player light').enabled = true;
  }
      else if (this.app.keyboard.wasPressed(pc.KEY_F)){
        
        this.entity.root.findByName('Player light').enabled = false;

        }
      
    
};

Here’s the script i created but Im sure something is missing or wrong.

Nvm i got it!

here’s the fix I did if anyone is looking for this

var FlashlightController = pc.createScript('flashlightController');

FlashlightController.prototype.initialize = function() {
    this.light = this.entity.root.findByName('Player light');
    this.light.enabled = false;
};

FlashlightController.prototype.update = function(dt) {
    if (this.app.keyboard.isPressed(pc.KEY_F)) {
        if (!this.keyDown) {
            this.light.enabled = !this.light.enabled;
            this.keyDown = true;
        }
    } else {
        this.keyDown = false;
    }
};

Note: attach the script to the player

1 Like