# Move vector A to vector B but stop X distance before reaching vector B and always be in front

I believe this is basic vector math, but I'm struggling with it.

I have 2 objects (3D space, x, y, z), A (weapon) and B (camera). I want to move A to B's location, but then offset A from B so that it is always in view of the camera.

endPosition = objA.worldPosition
startPosition = objB.worldPosition
distance = endPosition - startPosition

objA.worldPosition = distance


How do I offset object A so that it will always be an certain distance away from B so it will be in view?

To give you an idea of what I am doing... I have a weapon in the world that the player can click on and inspect to modify from anywhere (no fixed rotation or position). I want to move this weapon closer to the camera so they can turn it around, add attachments etc. Right now the weapon clips into the camera so I need to apply an offset.

You can use the normalized distance vector (actually a direction vector) in order to offset the weapon after moving it on top of the camera. Also, moving the weapon on top of the camera as a first step does not require the distance vector itself, you can directly assign the camera position to the weapon.
direction = normalize(objA.worldPosition - objB.worldPosition) // get the direction between the camera and the weapon