# Calculate Point between camera position and target position

**URL:** <https://discourse.threejs.org/t/calculate-point-between-camera-position-and-target-position/21156>\
**Category:** Questions\
**Tags:** math\
**Created:** [December 1, 2020, 6:15pm UTC](https://discourse.threejs.org/t/calculate-point-between-camera-position-and-target-position/21156 "2020-12-01T18:15:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwmatthias](https://avatars.discourse-cdn.com/v4/letter/g/ee59a6/32.png) [@gwmatthias](https://discourse.threejs.org/u/gwmatthias)\
**Post date:** [December 1, 2020, 6:15pm UTC](https://discourse.threejs.org/t/calculate-point-between-camera-position-and-target-position/21156/1 "2020-12-01T18:15:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [December 1, 2020, 6:21pm UTC](https://discourse.threejs.org/t/calculate-point-between-camera-position-and-target-position/21156/2 "2020-12-01T18:21:35Z")

</div>

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

```javascript
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?

---

<div class="post-metadata">

**Author:** ![gwmatthias](https://avatars.discourse-cdn.com/v4/letter/g/ee59a6/32.png) [@gwmatthias](https://discourse.threejs.org/u/gwmatthias)\
**Post date:** [December 1, 2020, 6:29pm UTC](https://discourse.threejs.org/t/calculate-point-between-camera-position-and-target-position/21156/3 "2020-12-01T18:29:09Z")

</div>

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 🙂  
I will try that …

Mat

---

<div class="post-metadata">

**Author:** ![gwmatthias](https://avatars.discourse-cdn.com/v4/letter/g/ee59a6/32.png) [@gwmatthias](https://discourse.threejs.org/u/gwmatthias)\
**Post date:** [December 1, 2020, 6:59pm UTC](https://discourse.threejs.org/t/calculate-point-between-camera-position-and-target-position/21156/4 "2020-12-01T18:59:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [December 1, 2020, 7:24pm UTC](https://discourse.threejs.org/t/calculate-point-between-camera-position-and-target-position/21156/5 "2020-12-01T19:24:54Z")

</div>

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](https://codepen.io/mjurczyk/pen/bGwdXpe?editors=0010).

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](https://threejs.org/docs/#api/en/math/Vector3.sub).

---

<div class="post-metadata">

**Author:** ![gwmatthias](https://avatars.discourse-cdn.com/v4/letter/g/ee59a6/32.png) [@gwmatthias](https://discourse.threejs.org/u/gwmatthias)\
**Post date:** [December 1, 2020, 8:24pm UTC](https://discourse.threejs.org/t/calculate-point-between-camera-position-and-target-position/21156/6 "2020-12-01T20:24:11Z")

</div>

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

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

https://codepen.io/gwmatthias/embed/preview/dypoxNL?height=300&slug-hash=dypoxNL&default-tabs=js,result&host=https://codepen.io

Thanks again mjurczyk!  
All the best  
Mat
