Set camera facing

Hello again you amazing people :slight_smile:

I am attempting to set the “facing” direction of the camera. I store out the direction and then want to be able to place the user back into that position and facing after some other things have occurred.

My current attempt was to use camera.getForwardRay().direction and then camera.setTarget(<dir>) and this does not seem to be working. Am I using an incorrect piece of data for the “facing” or should I not be using setTarget?

Current simple playground: Babylon.js Playground

Thank you for your time and help :slight_smile:

Hi @buckeyez, maybe you can take a look at this article from @PolygonalSun which goes in details into cameras: Cameras: Is what you see, what you get? | by Babylon.js | Medium. If you can’t find a solution, he might be able to help you too.

1 Like

Camera.setTarget() should work. The reason it is not working for you is because the value of dir is not changing over time. I modified your playground example to generate random targets and it works.

Again, remember that setTarget() takes a point in space (or an AbstractMesh) and not a direction and there is a distinction between point and direction although both are represented as Vector3.


1 Like

I thought I should add this for clarity. If your camera is positioned at point p and you want to look at direction dir from its current position, then you can achieve it by calling

camera.setTarget( p.add(dir) );


Thanks! That is what I was looking for!

1 Like