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.


1 Answer 1


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
objA.worldPosition = objB.worldPosition // move the weapon on top of the camera
objA.worldPosition = objA.worldPosition + direction * 10 // 10 is the number of 'units' you want to offset the weapon from the camera, you should decide this via trial and error

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .