I need code to make guns be able to picked up

i have a gun, but that is it. it wont shoot and cant be picked up. i also need guns to drop to the floor.

you need two objects, one as player child and one as external object, the world object can be picked up or clicking over it or by collision with player (automatic pick up) when you pick it up you disable the external entity and enable the child of player. The contrary when you drop it.

1 Like

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