I need code to make guns be able to picked up

Sample ---- ↓


π—œπ—»π˜ƒπ—²π—»π˜π—Όπ—Ώπ˜†.π—·π˜€
var Inv = pc.createScript("inventory");

Inv.GunIds = this.app.assets.filter(function(asset){
     return asset.tags.has("myGunTag");
});

Inv.prototype.initialize = function(){
    this.currentGun = -1;
};

Inv.prototype.update = function(dt){
    this.once("picked_item",function(entity){
        var index = Inv.GunIds.indexOf(entity.model.asset.id);
        if(index > -1 && this.currentGun != index){
         this.currentGun = index;
         // ------------ Enable the Gun here 
        }else if(index === -1){
         // ------------ Disable all Guns
        }
    });

    // Press Q to de-equip the gun
    if(this.app.keyboard.wasPressed(pc.KEY_Q) && this.currentGun != -1){
      var oldGunIndex = this.currentGun;
      // reset gun to nothing
      this.currentGun = -1; 
      // Hide all guns here
      

      // Test --------------->
      /* // Instance a new gun model into the world with a rigidbody
      var inst = new pc.Entity();
      var root = this.app.root.findByName(this.entity.root.name);
      inst.addComponent("collision");
      inst.addComponent("rigidbody");
      inst.collision.type = "sphere";
      inst.rigidbody.type = pc.BODYTYPE_DYNAMIC;
      root.addChild(inst);
      var mdl = new pc.Entity();
      mdl.addComponent("model");
      mdl.model.asset = Inv.GunIds[oldGunIndex-1];
      inst.addChild(mdl);*/
    }
};

π—£π—Άπ—°π—Έπ˜‚π—½.π—·π˜€
var pickup = pc.createScript("pickup");

pickup.attributes.add("trigger",{type:"entity"});
pickup.attributes.add("gunModel",{type:"entity"};
var destroy = false;
pickup.prototype.initialize = function("pickup"){
     this.trigger.collision.once("triggerenter",function(e){
           if(e.other.script.inventory){
                 var script  = e.other.script.inventory;
                 script.fire("picked_item",this.gunModel);
                 destroy = true;
           }
     },this);
};

pickup.prototype.update = function(dt){
    if(destroy === true){
      this.entity.destroy();
    }
};


You need to edit some parts for it to work

1 Like