Shooting a projectile following the rotation of a camera

My character has a 1st person POV and I would like to shoot my projectile following its movement, targeting the crosshair that appears in the screen. Right now, it will always shoot in a straight line at a fixed point. I know that I have to change the this.entity.translate() function to adjust accordingly to the camera’s rotation and movement, but I am having a hard time figuring out how.
I would appreciate any help that you can give me.
Thanks!

Link: PlayCanvas 3D HTML5 Game Engine

Hey @Johan_Guillen ! Welcome to playcanvas! Have you tried making the bullet teleport to the crosshair and then shoot?

1 Like

Hey @Codeknight999 ! Thanks for your reply!
I haven’t tried that, but I believe the issue is with the this.entity.translate() function that I am using to shoot the bullet.
If I teleport the bullet to the crosshair, it will start at that position but it will still shoot at the same fixed direction.
What I want is to shoot at the direction where the crosshair is looking at, the crosshair can move.
Thanks!

This may be a bit wonky, but I think it should work:
Instead of using translate use translateLocal. Translate local should take into account other factors such as rotation. If you do that, you can get the rotation of the crosshair and have your bullet always set that rotation to that. If you don’t know, you can use getRotation and setRotation.

1 Like