How do you make aiming on a viewmodel gun?

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.

It does not work and only works when you look straight.
https://streamable.com/1lp6nj

Here is my code

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
Dummy.HumanoidRootPart.CFrame=((Camera.CFrame-Camera.CFrame.upVector*1.3)*CFrame.Angles(movementModel.x,movementModel.y,movementModel.z))*Offset
6 Likes

Anyone know why its happening? Is there something wrong with my math

4 Likes

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
4 Likes

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

https://streamable.com/1lp6nj

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

1 Like

Delta time parameter can also be manipulated if the client fps is low.

1 Like

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

Forward:
image
Backward the same but when i look sideways:

1 Like

Try forcing the values to always be negative like this:

			local DistX = -Dist.X
			local DistY = -Dist.Y 
1 Like

Now it just did what happened before but in the opposite direction.

Try doing this

DistX = Camera.CFrame.RightVector*Dist.X
DistY = Camera.CFrame.UpVector*Dist.Y
1 Like

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

They viewmodel just stays still.

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
4 Likes

Wow thanks! It works perfectly:

3 Likes