Move the camera forward in the direction its facing

hello

i build a buttons in js that move the camera in rotation left and right and forward , but when i rotate the
the camera, and try to move forward, the forward motion not goes in the direction that the camera is facing, how can i fix this.

thanks

Hi!
Would be cool to see what and how you do in the code.

Take a look at these methods: .translateX, .translateY, .translateZ.

You can also use Camera.getWorldDirection() and then translate the camera along the resulting direction vector.

3 Likes

thanks ,
can you show my example to how to work with Camera.getWorldDirection()] ?

camera.position.add( camera.getWorldDirection() );

JSFiddle.

1 Like

But maybe don’t create a object every time., reuse a Vector3 instead like this:

const direction = new THREE.Vector3;

let speed = 1.0;

function eventOrLoopOrSomething() {

    camera.getWorldDirection(direction);

    camera.position.addScaledVector(direction, speed);

}
5 Likes

But maybe don’t create a object every time.

Good point :v: updated JSFiddle.
Forgot that getWorldDirection creates a new vector internally when target is not provided.

function eventOrLoopOrSomething()

LMAO :rofl:

3 Likes

thanks

i can move forward when i rotate the camera, but still cannot find solution to the issue when i rotate the camera
and try to move right or left or backward the direction is all off, its like the axe is not updating the camera rotation position,
any help will be appreciated

Not sure if it helps but I always create a Object3D instance where I want the camera and then parent this to something that is moving in the scene, then lerp the camera to the dummy and use lookAt to orientate the camera to a target. Not the same thing as what you are doing but maybe one possible work around https://codepen.io/nik-lever/pen/PrEvKb

1 Like

thanks for the help found the solution
using camera.translateZ( distance );
fix it , now is going the right direction