Hi. I’m working on a third-person shooter project. Tell me, please, how to implement the algorithm of shooting correctly? The more the better. Thank.
It’s a bit tricky doing a third person ‘shot’ and it’s normally done in 2 stages.
- Work out what the player is pointing at with the camera
- Firing at it with the characters weapon in the scene
Stage 1: assuming you are using physics, do a raycast in the direction that camera is facing with a max ray length (this varies on your game). If it hits something, keep track the position of the intersection. If not, keep track of the position of the end of the ray.
Stage 2. Do a raycast from the weapon to the point mentioned in Stage 1. This represents your ‘shot’
I made an invisible aim in the middle of the screen using the graphicsDevice methods.width and graphicsDevice.height. Then, using the screenToWorld() method, I set the endpoint. As a starting point, I take the coordinates of the bullet. Using the method raycastFirst(), I build the red ray, which is directed to the endpoint. The Ray() method, I use to build the blue ray when raycastFirst() returns null. When you click on the left mouse button and hover the aim in the center of the screen, displays ray. However, when applying the impulse, the bullet flies anywhere. Could you help me sort out with force and the correct flight path? Thanks.
Link to the project: https://playcanvas.com/editor/scene/674481