Help with making aiming possible

Is there a way to edit this code so when i hold mousebutton2 the camera cframe is a parts cframe so it resembles aiming a gun?

The script is for a viewmodel for my gun system.

local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Camera = workspace.Camera
local RunService = game:GetService("RunService")
local uis = game:GetService("UserInputService")

local ViewModel

local equipped = false

tool.Equipped:Connect(function()
	equipped = true
	game.ReplicatedStorage.ViewModelM4A1:Clone().Parent = Camera
	
end)

tool.Unequipped:Connect(function()
	equipped = false
	ViewModel = Camera.ViewModelM4A1
	ViewModel:Destroy()
end)

local swayCF = CFrame.new()

RunService.RenderStepped:Connect(function()
	if player.Character.Humanoid.Health <= 0 then
		if Camera:FindFirstChild("ViewModelM4A1") ~= nil then
			workspace.Camera.ViewModelM4A1:Destroy()
		end
	end

	if equipped == true then
		if Camera:FindFirstChild("ViewModelM4A1") ~= nil then
			Camera.ViewModelM4A1:SetPrimaryPartCFrame(Camera.CFrame)
			for i, v in pairs(Camera.ViewModelM4A1:GetChildren()) do
				if v:IsA("BasePart") then
					v.CanCollide = false
				end
			end

			local mouseDelta = uis:GetMouseDelta()/50

			local swayX = math.clamp(mouseDelta.X, -0.2,0.2)
			local swayY = math.clamp(mouseDelta.Y, -0.2,0.2)

			swayCF = swayCF:Lerp(CFrame.new(swayX, swayY, 0), .3)

			Camera.ViewModelM4A1:SetPrimaryPartCFrame(Camera.CFrame * swayCF)
		end
	end
end)

Any help will be useful!

1 Like

Make a second part in the viewmodel which is used as the point for the camera to be in when aiming. Same as the primary part. Make sure to tween it. Or you can make an aiming animation. Like any sane person would do. (definitely me)

Make sure to rig the viewmodel first though, also make an animator or animationcontroller.

For the input, use UIS:IsMouseButtonPressed().

I did this but its just bringing the gun a lil bit up (aimpart pos is good) stops the viewmodel from rotating and stops the animations of it working. Can u tell me whats wrong?

local tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Camera = workspace.Camera
local RunService = game:GetService("RunService")
local uis = game:GetService("UserInputService")

local ViewModel
local equipped = false
local aiming = false

tool.Equipped:Connect(function()
	equipped = true
	local viewModelClone = game.ReplicatedStorage.ViewModelM4A1:Clone()
	viewModelClone.Parent = Camera
	ViewModel = viewModelClone
end)

tool.Unequipped:Connect(function()
	equipped = false
	if ViewModel then
		ViewModel:Destroy()
		ViewModel = nil
	end
end)

uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
			aiming = true
		end
	end
end)

uis.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		aiming = false
	end
end)

local swayCF = CFrame.new()

RunService.RenderStepped:Connect(function()
	if player.Character.Humanoid.Health <= 0 then
		if Camera:FindFirstChild("ViewModelM4A1") then
			Camera.ViewModelM4A1:Destroy()
		end
	end

	if equipped and ViewModel then
		local targetCFrame = Camera.CFrame

		if aiming then
			if ViewModel:FindFirstChild("AimPart") then
				ViewModel:SetPrimaryPart(ViewModel.AimPart)
			end
		else
			ViewModel:SetPrimaryPartCFrame(targetCFrame)
		end

		for _, v in pairs(ViewModel:GetChildren()) do
			if v:IsA("BasePart") then
				v.CanCollide = false
			end
		end

		local mouseDelta = uis:GetMouseDelta() / 50
		local swayX = math.clamp(mouseDelta.X, -0.2, 0.2)
		local swayY = math.clamp(mouseDelta.Y, -0.2, 0.2)
		swayCF = swayCF:Lerp(CFrame.new(swayX, swayY, 0), .3)

		if not aiming then
			ViewModel:SetPrimaryPartCFrame(targetCFrame * swayCF)
		end
	end
end)

You’re changing the primary part, but you’re not positioning it correctly. Look at line 56. You don’t have to put an else there, since you’re going to be changing the primary part’s CFrame anyway. Errors happen, I guess.