Bullet script doesn't work

image

Save the script first. Then parse the script, remove the script and add the script again.

I not mean remove the code. Remove the script from the entity and add the script again.

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

// initialize code called once per entity
Bullet.prototype.initialize = function() {
    this.entity.collision.on('collisionstart', this.onCollisionStart, this); 
    this.entity.collision.on('collisionend', this.onCollisionEnd, this);
};

// update code called every frame
Bullet.prototype.update = function(dt) {
    if (this.entity.enabled === true) {
        this.entity.translateLocal(0,0,-0.2);
        
        setTimeout(function(){
            this.entity.destroy();
        }.bind(this), 3000);
    }
};

Bullet.prototype.onCollisionStart = function (result) {
    if (result.other) {
        this.entity.destroy();
    }
};

done

Sorry to say, but you need to redo the steps again. Parse. Remove. Add.

This time you removed the script from the project, you only need to remove it from the script component of the entity.

ok i parse it removed it and added it

And it works. :partying_face:

it still not it only fires one then disappears

see

Yes, that’s all you’ve coded so far.

how would i make it fire multiple
@Albertos

I’m afraid that’s a little bit difficult to understand if you don’t have code knowledge yet.

You need to disable the original bullet (to prevent it’s used as a bullet). Then when the user shoot you need to clone (copy) this bullet. Add it as a child to the Root entity of your scene. Then apply the correct position and rotation (most likely based on the original bullet) and at least enable it, so the script you have made can do it’s job.

ok is there any projects with examples

I’ve applied it in a fork of your own project. Most logic is in the code below. I also changed your bullet setup a bit. You can fork the project if you want. Please let me know when you are finished, because then I will remove the project.

image

https://playcanvas.com/project/936092/overview/shooting