How do I shoot projectiles/bullets in my FPS Game?

As some of you in the forums already know, I’m creating an FPS game and first I’m making the prototype. The last step to finish it is to shoot bullets out of a gun but I couldn’t make it work so I’m asking here for help.

The link to my project : FPS Game by VisionsKlips

Help would be greatly appreciated!

1 Like

Hi @VisionsKlips!

I recently made a basic shooting example project that maybe can help you.

https://playcanvas.com/project/1267420/overview/basic-shooting

1 Like

I will see if I can implement this into my project.

1 Like

Can you give more information on how reparenting works?

With reparent() you change the parent of the entity in the hierarchy. If the entity has no parent yet, it works just like addChild().

And because I am creating my own shooting script suite is there anything specific you think I should add?

I think most users want to see a nice shooting effect. I’m personally thinking about an example using raycast, but I’m not sure about this effect yet. The problem is that you get the result of a raycast instantly, while the moving projectile is not arrived yet. So using a moving projectile as bullet is not really an option in combination with a raycast. At least not for a simple example.

I just used a very long thin black box as a bullet trace. Perhaps placing some sort of particle effect where the bullet hits would be good.

Yes, that’s a good idea. I was thinking of something similar too. :ok_hand:

Well I did use it, but then I got rid of it with the new raycast because it went through walls. I could theoretically add it back in and just resize it to match the distance to the hit point but considering some objects like grenades cast 360 rays that would be a calculation nightmare. Oh and I just realized that all those calculations happen in the same frame so it would just uselessly move the box around until it gets rendered at its final rotation.

Could you please provide a script especially for my project as the clone system isn’t working.

It gives an error of : Uncaught TypeError: Cannot read properties of null (reading ‘clone’)

The Bullet entity should be a child of your Pistol entity.

Now it is giving me this error:

[bullet.js?id=202791148&branchId=78c035bd-f9b9-4aad-b2e4-edc164eebe81:27]: Uncaught TypeError: Cannot read properties of undefined (reading ‘name’)

TypeError: Cannot read properties of undefined (reading ‘name’)

when the Player name is the same in both projects. I deleted the enemy one because I do not have one but it still gives this error.

Can you please give me code that I can use by forking my project please? It would be really helpful as when I fix mistakes, other mistakes are occuring.

Can you please add the code of the original script again? Even without an enemy in your scene, the script should work without errors.

1 Like
var FirstPersonMovement = pc.createScript('firstPersonMovement');

FirstPersonMovement.attributes.add('camera', {
    type: 'entity',
    description: 'Optional, assign a camera entity, otherwise one is created'
});

FirstPersonMovement.attributes.add('power', {
    type: 'number',
    default: 2500,
    description: 'Adjusts the speed of player movement'
});

FirstPersonMovement.attributes.add('jumpImpulse', {
    type: 'number',
    default: 3,
    description: 'Adjusts the impulse of player Jump'
});

FirstPersonMovement.attributes.add('gravity', {
    type: 'number',
    default: 90,
    description: 'Adjusts the gravity'
});

FirstPersonMovement.attributes.add('lookSpeed', {
    type: 'number',
    default: 0.25,
    description: 'Adjusts the sensitivity of looking'
});

FirstPersonMovement.prototype.initialize = function () {
    this.force = new pc.Vec3();
    this.eulers = new pc.Vec3();
    this.canJump = 1; // Use this or I suggest this.canJump = true;

    var app = this.app;

    // Listen for mouse move events
    app.mouse.on("mousemove", this._onMouseMove, this);

    // when the mouse is clicked hide the cursor
    app.mouse.on("mousedown", function () {
        app.mouse.enablePointerLock();
        console.log("Mouse Clicked");

    


    }, this);

    // Check for required components
    if (!this.entity.collision) {
        console.error("First Person Movement script needs to have a 'collision' component");
    }

    if (!this.entity.rigidbody || this.entity.rigidbody.type !== pc.BODYTYPE_DYNAMIC) {
        console.error("First Person Movement script needs to have a DYNAMIC 'rigidbody' component");
    }
};






//update code called every frame
FirstPersonMovement.prototype.update = function (dt) {
    
    if (!this.camera) {
        this._createCamera();
    }

    var force = this.force;
    var app = this.app;

    
    var forward = this.camera.forward;
    var right = this.camera.right;
    let power = this.power;

    // movement
    var x = 0;
    var z = 0;

    // Use W-A-S-D keys to move player
    // Check for key presses
    if (app.keyboard.isPressed(pc.KEY_A) || app.keyboard.isPressed(pc.KEY_Q)) {
        x -= right.x;
        z -= right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_D)) {
        x += right.x;
        z += right.z;
    }

    if (app.keyboard.isPressed(pc.KEY_W)) {
        x += forward.x;
        z += forward.z;
        power = this.power * (app.keyboard.isPressed(pc.KEY_SHIFT) ? 3 : 1);
        console.log('power: ', power);
    }

    if (app.keyboard.isPressed(pc.KEY_S)) {
        x -= forward.x;
        z -= forward.z;
    }

    if (app.keyboard.isPressed(pc.KEY_SPACE) && this.canJump == 1) {
        this.entity.rigidbody.applyImpulse(0, this.jumpImpulse, 0)
    }

   
    if (x !== 0 || z !== 0) {
        force.set(x, 0, z).normalize().scale(power);
        this.entity.rigidbody.applyForce(force);
    }

   
    if (this.entity.getPosition().y > 3) {
        this.canJump = 0

        
    }

    if (this.entity.getPosition().y <= 3) {
        this.canJump = 1

        
    }

    if (this.canJump == 0) {
        
        this.entity.rigidbody.applyForce(new pc.Vec3(0, -this.gravity, 0));
    }


  
    this.camera.setLocalEulerAngles(this.eulers.y, this.eulers.x, 0);
};

FirstPersonMovement.prototype._onMouseMove = function (e) {
    
    if (pc.Mouse.isPointerLocked() || e.buttons[0]) {
        this.eulers.x -= this.lookSpeed * e.dx;
        this.eulers.y -= this.lookSpeed * e.dy;
    }
};

FirstPersonMovement.prototype._createCamera = function () {

    this.camera = new pc.Entity();
    this.camera.setName("First Person Camera");
    this.camera.addComponent("camera");
    this.entity.addChild(this.camera);
    this.camera.translateLocal(0, 0.5, 0);
};

Thank you for helping me a lot @Albertos !

Here is a fixed version.

https://playcanvas.com/editor/scene/2106787

  • I removed the code part that gives the error, because I was unable to find the reason for now.

  • I changed to rigidbody type of the bullet to kinematic, like the example project.

  • I changed the bullet rotation and the movement direction.

1 Like

The error is that they didnt name the bullet, also you should not use findbyname you should use an attribute entity.