Camera Direction Vector


#1

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


#2

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.


#3

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]


#4

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