Camera Direction Vector

Hey,

I’m working on an FPS and I was wondering how to get the vector the camera is looking at.
I’d like to use the vector to spawn a projectile.

The code is already working, but only if I face a certain direction.
Since it’s a shooter, I want the projectiles to spawn in front of the player, no matter which direction they’re facing.

Any advice?

https://playcanvas.com/editor/code/585561?tabs=15558185,15359855
https://playcanvas.com/editor/scene/662336

If you have entity that the camera component is attached to, you can use cameraEntity.forward where cameraEntity is the entity that the camera component is attached to.

I have tried that.
Both the cam and the player forward always return the same vector. Shouldn’t the vectors change if I rotate my viewport?

No matter player position or direction they’re looking at, in debug I always get:
camera force is [0, 0, -1]
force of player is[0, 0, -10]

Looks like a weird editor/data binding bug.

For whatever reason, this.camera is null on FirstPersonMovement script despite what is shown in the editor. This meant that a new camera is created in the first update of the script which is the one being rendered and controlled by the mouse.

I cleared the entry in the script in the editor and reassigned it the Camera in the scene again. After that, it works as expected.

Nov-19-2018%2017-03-44

I tried it myself, AND I tested to see what direction my camera was facing (it outputs the correct direction), but my code still only works at a certain angle.

Here is my code:
https://playcanvas.com/editor/code/592819?tabs=16165198,16165194,16165166,16165165

Sorry, what context was this reply for?

I want to make the floating cube to the right of where the player starts change its material to the brick texture when you look at it but so far that only works when you are at a certain position relative to it

Nevermind, I got it to work!