How to create FPS projectiles?

I really do hope I’not asking a duplicate question, as it feels like this’d have been answered before.
But, I’ve been looking at the forum for a while now, and haven’t found what I’m looking for.
Every answer I can find include example projects people have sent as responses seem fairly outdated and give a load of error messages now, or something other of the like. There also doesn’t seem to be a basic FPS in the tutorials, so I’m asking here myself.

How do I shoot projectiles for a first-person shooter project, so starting at or just below the camera to the place where I’ve mouse clicked?

Hi @kajvanveen_dev,

Indeed an FPS tutorial or example comes up quite often, sadly there isn’t one available. To help you with your task, a good starting point is to break it in parts:

  1. How to get the start world position of the projectile based on the mouse cursor position? You are looking for a screen to world example, here is one: Entity picking without physics | Learn PlayCanvas

  2. Then you need to find the direction of your projectile, this is simple enough, it’s the forward direction of your camera:

const direction = this.cameraEntity.forward;

You can use that direction as a starting point to calculate an impulse. The first person movement tutorial does something similar to move the player forward. This isn’t very different than shooting a projectile forward:

https://developer.playcanvas.com/en/tutorials/first-person-movement/

  1. As soon as you have your starting point, spawn/place your projectile there. You also have your impulse calculated from step 2., check this tutorial on how to apply it to your projectile:

https://developer.playcanvas.com/en/tutorials/Using-forces-on-rigid-bodies/

And hopefully you will see it go forward. Make sure to destroy it as soon as it hits something, check this collision events example on that:

https://developer.playcanvas.com/en/tutorials/collision-and-triggers/

1 Like

Check this post @Tirk182 shared, it includes an actual example:

1 Like