I am trying to make the viewmodel of my gun move to where the camera is looking. I have tried getting the offset between a part on the viewmodel called AimPart and setting an offset to the viewmodel to the distance between them.
if not Aiming then
local Dist = (Camera.CFrame.Position-Dummy.Client.AimPart.Position)
Offset=CFrame.new(Dist.X,Dist.Y,0)
Aiming=true
else
Offset=CFrame.new()
Aiming=false
end
Personally I use CFrame lerping in a renderstepped function. Here’s kinda what I mean, sorry if I’m a bad explainer.
local RS = game:GetService("RunService")
local aimCFrame = gun.AimPart.CFrame
local aiming = false
local aimCFrameGun = CFrame.new()
local cam = workspace.CurrentCamera
RS.RenderStepped:Connect(function()
gun:SetPrimaryPartCFrame(cam.CFrame
-- other frames
* aimCFrameGun) -- multiplies main gun cframe by the aiming cframe
if aiming then
aimCFrameGun = aimCFrameGun:Lerp(aimCFrame,.1) -- lerps the variable that was multiplied onto the main gun cframe
else
aimCFrameGun = aimCFrameGun:Lerp(CFrame.new(),.1)
end
end)
--obviously you'd have to make the aiming variable turn true/false
Yes I do the same thing but what I want to know is how do I calculate how to get the sight on the gun model to line up with the camera?
Everything im clicking mousebutton2 in the video its running the aim function.
if not Aiming then
local Dist = (Camera.CFrame.Position-Dummy.Client.AimPart.Position) -- Something is wrong here but I have no idea what it is.
Offset=CFrame.new(Dist.X,Dist.Y,0)
Aiming=true
else
Offset=CFrame.new()
Aiming=false
end
Try printing out the values like Dist and Offset. I think it might be that when you are doing the Dist operation and look towards a different space in the world, it is messing up your calculations and inverting the scope Cframe math.
What I mean is that when you look towards the dummies, the math might be 5-2 = 3, but then when you turn around it might be -5-2 = -7 and invert the offset if that makes sense. Try that
Ive done what you said and it works when I look back and forth but not left and right?
if not Aiming then
local Dist = (Camera.CFrame.Position-Dummy.Client.AimPart.Position)
local DistX = Dist.X
local DistY = Dist.Y
if DistX > 0 then
DistX=-DistX
end
if DistY > 0 then
DistY=-DistY
end
Offset=CFrame.new(DistX,DistY,0)
Aiming=true
else
Offset=CFrame.new()
Aiming=false
end
I am 100% sure this should have done something but it didn’t and I am completely confused why.
Now it just absolutely does nothing. Doesn’t move. I tried making them negative still nothing
I’m not that good at ToObjectSpace and ToWorldSpace so you might need to switch around which goes where for the ToObjectSpace part but this might work I hope
if not Aiming then
local Dist = Camera.CFrame:ToObjectSpace(Dummy.Client.AimPart.Position)
Offset=CFrame.new(Dist.X,Dist.Y,0)
Aiming=true
else
Offset=CFrame.new()
Aiming=false
end
Dummy.HumanoidRootPart.CFrame=(Camera.CFrame
* CFrame.new(0,-1.3,0)
* CFrame.Angles(movementModel.x,movementModel.y,movementModel.z))
* Offset