I’m currently trying to position an object in front of the camera, then move and rotate it so that it lines up with the same rotation and position it had relative to the player when they prompted to pick it up.
I’ve been able to calculate the correct displacement between the center of the object and the intersect of the ray (cast from the camera forward), however once I rotate the object the displacement I calculated no longer lines up because it is not rotated with the object.
Code to calcuate the rotation and displacement of the object relative to the camera:
local camX,camY,camZ = Camera.CFrame:ToEulerAnglesYXZ()
self.RotX = math.rad(self.object.Orientation.X) - camX
self.RotY = math.rad(self.object.Orientation.Y) - camY
self.RotZ = math.rad(self.object.Orientation.Z) - camZ
-- Calculate the distance between where the intersect is to where the center of the item is
self.displacement = self.object.Position - intersect -- Vector3
Code to position the object in front of the player:
-- Updates the CFrame of the item to in front of the player's camera
function CarryController:moveObject()
local newOffsetZ = defaultOffsetZ
local nextObject : RaycastResult = checkFront()
if nextObject then
local offsetClamp = math.clamp(nextObject.Distance,MinOffsetZ,math.huge) -- Drag object in front of anything behind it
newOffsetZ = math.clamp(newOffsetZ,MinOffsetZ,offsetClamp)
end
local compiledOffset = Vector3.new(0,0,-newOffsetZ) + self.displacement
self.object.CFrame = Camera.CFrame * CFrame.new(compiledOffset) * CFrame.Angles(self.RotX,self.RotY,self.RotZ)
end
How do I rotate my Vector3 displacement by the same amount as the CFrame?.