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

lets say i soot north, then i look west, the bulet will move with my cusor from north to west

I changed the speed a bit. I don’t know what you mean by the other.

The bullet won’t continue going north when I look west

Like when you do a flick, the bullet hits the guy while you already look forward, but you can’t do that because the bullet will still be going towards the crosshairs, it’s hard to explain

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.