Coding gone wrong

hay this all i want is code to make bullet examle when mouse pressed clone then wait 0.01 sec then fire in the direction im looken at and after a while it stops deleates its self to stop lag

my code pls fix bullet is bad i try do it for my game bullet here is the code try fix make it like working bullet what i said

this.onKeyPress("mouse", function () {
    this.clone("self");
    for (var _repeatIdx1=0; _repeatIdx1<1000; _repeatIdx1++) {
        this.wait(0.001);
        this.motion.move(6.7);
    }
    this.deleteEntity();
});

also im just like 10 yrs is that why im bad at code

anything wrong tho with the code

To fire a bullet when you click the mouse you can add a new script with the name “mouse” to your player entity and replace the code with the code below.

var Mouse = pc.createScript('mouse');

// initialize code called once per entity
Mouse.prototype.initialize = function() {   
    this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
};

Mouse.prototype.onMouseDown = function (event) {
    if (event.button === pc.MOUSEBUTTON_LEFT) {
        var bullet = this.app.root.findByName('Bullet').clone();
        this.app.root.addChild(bullet);
        bullet.setPosition(this.entity.getPosition());
        bullet.setRotation(this.entity.getRotation());
        bullet.reparent(this.app.root);
        bullet.enabled = true;
    }
};

To make this work you need an entity with the name “Bullet”. Add a new script with name “bullet” and replace te code with the code below. Add also a collision and rigidbody component. Set the rigidbody type to kinematic. If you want you can also add a sphere model, otherwise you can’t see the bullet.

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();
    }
};

I expect that some adjustments are needed to get the desired result. You can also use the scripts as an example and do it your own way.

Please don’t multipost @Jesse_Gagne. Just edit your most recently post please

k