Shooting features

This is my code for the bullet so far:

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

// initialize code called once per entity
Bullet.prototype.initialize = function() {
    if(this.entity.enabled){
        this.entity.sound.play("pistol shooting sound effect");
    }

    //Registriert die Kugel eine Kollision, verschwindet sie 3 Sekunden später
    this.entity.collision.on('collisionstart', this.onCollisionStart, this); 
};

// update code called every frame
Bullet.prototype.update = function(dt) {
    //Davor war "enabled" bei der Kugel deaktiviert, da es ansonsten Probleme mit der Kollision mit dem Spieler gegeben hätte
    if (this.entity.enabled === true) {
        //Die Kugel wird korrekt ausgerichtet, damit sie von der Kamera aus "geradeaus" geschossen wird
        this.impulse = new pc.Vec3();
        this.impulse.copy(this.entity.forward).scale();
        this.entity.rigidbody.applyImpulse(this.impulse);

        //Selbstzerstörungstimer
        setTimeout(function(){
            this.entity.destroy();
        }.bind(this), 7500);
    }
};

//Verkürzter Selbstzerstörungstimer, falls die Kugel eine Kollision erkennt
Bullet.prototype.onCollisionStart = function (result) {
    if (result.other) {
        setTimeout(function(){
            this.entity.destroy();
        }.bind(this), 3000);    }
};

Don’t worry, most of the comments are in German, but the code can explain itself.

Anyway, what I’m struggling with is to…

  1. …get the bullet to be shot in the direction I watch (up and down included).
  2. …give the bullet only a short impulse, so it can fall down like a ball that has been thrown

I want both of the problems to be solved in the beginning of the update function, right after the ‘enabled’ check.

Thanks in advance

Hi @SayHiToMePls! Right now you are applying the force in the update function of the bullet itself. That means it’s repeated constantly. I would apply a forward force on the point where you fire the bullet.

And how do i specifically do that?

You can also do it in the initialize function where you start the sound. Just check which axis moves your bullet forward. Apply a force on that axis. Play around with the amount of force (negative or positive) to get the right result.

this.entity.rigidbody.applyImpulse(0, 0, -100);

Oh, ok! I will try it out rn

Can I maybe get the pistol to shoot with a predefined impulse, based on the direction of where I’m looking at, combined with the speed it has at the moment?

Yes, you can do that.

Yes, I did it already. I was gonna delete my reply, but then i got too lazy to do so

Ok, now I ran into some problems with my code that I can’t fix.

How can I shoot my bullet with momentum. For example:

  1. I move back to the right, so when I shoot and then stop, the bullet doesn’t reach very far and keeps on going to the right.
  2. I go for forward, jump and shoot while I’m still elevating. Then, the bullet should reach farther and higher than normal.

How do I implement this. I’ve deleted what I tried before to start from scratch:

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

// initialize code called once per entity
Bullet.prototype.initialize = function() {
    if(this.entity.enabled){
        this.entity.sound.play("pistol shooting sound effect");

        this.impulse = new pc.Vec3();
        this.impulse.copy(this.entity.forward).scale(30);
        this.entity.rigidbody.applyImpulse(this.impulse);
    }

    //Registriert die Kugel eine Kollision, verschwindet sie 3 Sekunden später
    this.entity.collision.on('collisionstart', this.onCollisionStart, this); 
};

// update code called every frame
Bullet.prototype.update = function(dt) {
    //Davor war "enabled" bei der Kugel deaktiviert, da es ansonsten Probleme mit der Kollision mit dem Spieler gegeben hätte
    if (this.entity.enabled) {
        //Selbstzerstörungstimer
        setTimeout(function(){
            this.entity.destroy();
        }.bind(this), 7500);
    }
};

//Verkürzter Selbstzerstörungstimer, falls die Kugel eine Kollision erkennt
Bullet.prototype.onCollisionStart = function (result) {
    this.entity.sound.play("ball collision sound effect");
    if (result.other) {
        setTimeout(function(){
            this.entity.destroy();
        }.bind(this), 3000);    }
};

Don’t worry, most of the comments are in German, but the code can explain itself, it’s for a thesis :wink:.

Sorry, I don’t understand your two questions and I don’t understand your logic. A bullet is only fired once, right? That would mean that you only apply a force once and not repeatedly?

What is the position of the bullet before you fire the bullet? If it is a child entity of the player entity, you get some problems when you move or rotate the player entity.

No, I want to influence the bullet’s impulse based on how it moved before. If it is too complex to implement a rotational and translational influence, I would just like to check if the player was going forward or backward

In other words: If I move forward, the impulse on the bullet is higher. If I move back, it should get lower.

Perhaps I did not express my question correctly, so I will try this another time:

I want to add some realism to my shooting mechanic. Namely, I want to apply my momentum from my movement to the bullet. If not too complex, I’d also let the bullet’s path be influenced based on the rotation at the moment.

I.e.,

  1. If I shoot while moving forward, the bullet should get farther. If I move backward, the bullet shouldn’t get very far.
  2. If I shoot while jumping, the bullet should get higher.
  3. Shooting while going sideways should influence the bullet’s movement respectively
  4. If we can get extra cool tonight, the bullet should swirl in the direction I’m turning (up, down and diagonally as well).

Just so you don’t ask questions concerning the bullet itself, here are the bullet’s properties that I’ve already implemented:

  1. You can think about the bullet I’m talking about as a tennis ball. Ergo, it has a dynamic rigid body, not a kinetic one.
  2. I give out an impulse on the bullet when I shoot it out.

Again, big thanks in advance and afterwards