Hello, as a lot of FPS programmers know (at least most) it gets very tiring eye-balling an aim offset for every individual gun!
That’s why I am looking for an equation (which would help a lot of people out with aim-offsets)
What I’m looking for is an efficient way that calculates the perfect offset for aiming when given a reference point which is an Aim-part (the part being where it should offset to). (utilizing standard viewmodels)
If you can help me with how I can possibly achieve this please share it! Thank you!
I understand that but if I were to utilize that method then I would have to eye-ball it and keep adjusting the CFrame till I get the offset i want. I was wondering if there was a formula or something that would automatically get the offset utilizing a reference point (which is an instance that it should ads to)
That is also a problem I have lol. But my main issue is finding the right CFrame to lerp to in order to position the viewmodel correctly to look like its aiming
If you have some reference part on the gun that says “this part should line up with the camera” you could calculate where the primary part should go like
local camera = workspace.CurrentCamera
local primaryPart = ...
local cameraAimPart = ...
local primaryPartOffset = cameraAimPart.CFrame:Inverse() * primaryPart.CFrame
local targetPositionForPrimaryPart = camera.CFrame * primaryPartOffset
Like, place the primaryPart such that it’s offset from the camera by the same amount that it’s currently offset from the cameraAimPart.
You’d just calculate the offset from cameraAimPart → primaryPart like
local primaryPartOffset = primaryPart.Position - cameraAimPart.Position
And then would add that position to the camera’s position:
local targetPositionForPrimaryPart = camera.CFrame.Position + primaryPartOffset
Done!
CFrames are extremely similar, conceptually. But they include rotations, so they don’t just have + and - operations.
The + becomes *.
Instead of -, you can * the Inverse() instead. For matrix multiplication reasons, the inverse goes on the left.
More info on the inverse: If CF is a CFrame that represents a rotation of 45 degrees clockwise and a movement of +5 studs on the x-axis. CF:Inverse() would be a CFrame that represents a rotation of 45 degrees counter-clockwise and a movement of -5 studs on the x-axis.
So I can think of Inversing a CFrame kind of like subtracting a CFrame from another to get the distance between that? Kind of like if you were subtracting numbers to see how far apart they are?