Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move the camera along its LookAt axis in Three.js?

Tags:

three.js

I'm new to Three.js, so if this is obvious then apologies upfront.

I set my camera using the LookAt method, and would like to move along that vector when pressing a key on the keyboard.

I've tried mimicking the feature by subtracting the camera vector from the point I'm looking at via subVector, normalized the resulting vector, then did something along the lines of:

camera.position.x += lookAtVector.x
camera.position.y += lookAtVector.y
camera.position.z += lookAtVector.z

...but I appear to be drifting a bit and not moving toward the lookAt point when I do this. How can I move along a lookAt vector in three.js?

like image 524
larryq Avatar asked Dec 30 '25 13:12

larryq


1 Answers

The camera is looking down it's negative-Z axis, so you want to do this:

camera.translateZ( - distance );

three.js r.57

like image 102
WestLangley Avatar answered Jan 06 '26 16:01

WestLangley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!