我相信这是基本的向量数学,但我在挣扎。
我有两个物体(3D空间,x,y,z),A(武器)和B(照相机)。我想把A移到B的位置,然后把A从B中偏移,这样它就总是在摄像机的视野中。
endPosition = objA.worldPosition
startPosition = objB.worldPosition
distance = endPosition - startPosition
objA.worldPosition = distance
我如何抵消物体A,使它总是离B一定的距离,所以它就在视野中?
让你知道我在做什么。我有一个武器在世界上,玩家可以点击和检查修改从任何地方(没有固定的旋转或位置)。我想把这个武器移近相机,这样他们就可以把它转过来,添加附件等。现在,武器剪辑进入相机,所以我需要一个偏移。
发布于 2021-03-09 02:56:21
你可以使用归一化的distance
矢量(实际上是一个direction
矢量)来抵消武器在摄像机上方移动后的偏移。另外,作为第一步,在摄像机顶部移动武器并不需要distance
矢量本身,您可以直接将相机位置分配给武器。
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
https://gamedev.stackexchange.com/questions/189691
复制相似问题