[SOLVED] How do I make a gun shoot, reload, and show magazine

It is not quite clear to me yet, but when you have fired a bullet you usually cannot change his direction anymore. The bullet goes straight and does not follow the player anymore. If you mean a wave effect you could get this if you fire a lot of bullets in a row while moving.

and there are a couple of other things I wanna cover I wanna show the magazine size on the onscreen HUD
the bullet thing link to youtube ( https://www.youtube.com/watch?v=7pcPkzn5shY )
and a little reload time and a little animation for the pistol.

this is my problem.https://youtu.be/7pcPkzn5shY
(i made a youtube video, Itā€™s still uploading.)

tell me when you see it, then i will take it down. (screen capture 5)

I see what is the problem. Can you show me the code where I clone the bullet please?

I suggest to search on the forum for how to display a value of a variable on your UI element or open a new topic about this.

You have to improve the current basic code by yourself. Search for example projects and other similar topics to help you started. If you really canā€™t figure it out, you can ask others for help.

ok,

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

is this what you mean?

No, I mean the part when the player shoot and a new bullet is created.


Movement.prototype.onMouseDown = function(event) {
    if (event.button === pc.MOUSEBUTTON_LEFT) {
        // shoot
        if (this.rounds !== 0) {
            var bullet = this.app.root.findByName('Bullet').clone();
            this.app.root.findByName('handgun').addChild(bullet);
            bullet.setPosition(this.app.root.findByName('Bullet').getPosition());
            bullet.enabled = true;
            console.log(bullet);
            // update magazine
            this.rounds = this.rounds - 1;
        }
    }
};

Could you replace that line with the line below please?

this.app.root.addChild(bullet);

Please also add the line below.

bullet.setRotation(this.app.root.findByName('Bullet').getRotation());

i did that
it still does the same as in the youtube video i posted

Then try to add the line below also.

bullet.reparent(this.app.root);
1 Like

Right after this

it is broke, i did what you said, and when i look north the bullet goes south, and when i look west the bullet goes south, IT ONLY GOES SOUTH

Have you add the setRotation line?

bullet.setRotation(this.app.root.findByName('Bullet').getRotation());

i only added what I was told to

i saw this do i mess with it? bullet.setPosition(this.app.root.findByName(ā€˜Bulletā€™).getPosition());

Thatā€™s for the the position. You also need it for the rotation.

Would it be risky to put the Bullet function as a a separate script attached to the handgun? Cuz I want a shotgun and that ainā€™t gonna work like that. Shottieā€™s holdingā€™ 2

And letā€™s say ima do a shotgun, could I make more than 1 pellet and they each go in different ways,?

First things first, is your rotation problem solved and works your normal shooting as you wish?

yes, they are still slow, how do I make them faster, is it a variable