Calculate Point between camera position and target position

Hey,

in my scene, i have different hotspots you can click on which activates a camera movement (Tweening) to this clicked hotspot.
Now I want the camera to stop let’s say 5 units before reaching the clicked hotspot.
I know this must be easy but I’m not able to calculate the correct point in space.

Camera position -> Object position -> get the distance between the two - subtract a fixed distance while maintaining the direction -> get this point as Vector3D.

Thanks a lot!
Mat

Since you know the direction, you can normalize that vector and use it to modify the offset.

const distance = camera.position.clone().sub(target.position); // Note: depending on the order of subtracting, you'll get either forward or backward direction
const direction = distance.normalize(); // Length of a normalized vector is always 1, while preserving vector direction

const offset = distance.sub(direction.multiplyScalar(5.0));

Is this what you had in mind?

Hey mjurczyk,

thanks for the quick reply!
If offset is a Vektor3D which is 5 units less far away from the camera position than the target position keeping the direction, than yes :slight_smile:
I will try that …

Mat

Unfortunately “distance” is always 0:

camera.position : x: 4.197376667562277 / y: 1.5258541901872889 / z: 9.888916856060966
target.position : x: -4.5 / y: -1.19 / z: -0.5
distance : x: 0 / y: 0 / z: 0

but why ???

should I use subVectors() instead?

The code above is an explanation - not a copy-paste solution. Please try to understand it, then write into your code in a way it’d fit.

See console in this example.

Most vector methods in three mutate the caller. If you just copy the code above, you may be zeroing the distance vector by following it up instantly with offset calculations.

of course, I do not just copy and paste the code - that was so rude :wink:
I simply put my consol.log in the wrong place …

I made a little CodePen with my idea and your solution for demonstration:

Thanks again mjurczyk!
All the best
Mat

1 Like