Trouble with ViewModel Aim Down Sights function

I’ve been trying to make my own gun system for the past few days (using tools) and I’ve come across a fork in the road when trying to make an Aiming Down Sights feature for my gun’s viewmodel. I tried watching some tutorials on making gun systems but I could not get any methods to work. How could I get the camera to re-adjust to the AimPart in my viewmodel?



(My code for aiming as well as all of my variables)


(AimPart turns red when I am supposed to be aimed in, and green when not aiming)

3 Likes

I personally have a “Head” part that is normally CFramed to the camera and an “Aim” part which I CFrame to the camera when the player is Aiming.

I’ll provide my aiming code:

			local camframe = game:GetService("Workspace").CurrentCamera.CFrame
			cam:FindFirstChild(curWpn).ADSPart.CFrame = camframe + camframe.LookVector * dM
			local rotation = workspace.CurrentCamera.CFrame:toObjectSpace(lastCameraCF)
			local x,y,z = rotation:ToOrientation() 
			swayOffset = swayOffset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1)
			cam:FindFirstChild(curWpn).ADSPart.CFrame = cam:FindFirstChild(curWpn).ADSPart.CFrame * swayOffset 
			lastCameraCF = workspace.CurrentCamera.CFrame
			isMoving()
			while cam.FieldOfView >= 46 do
				cam.FieldOfView -= 1
				task.wait(0.01)
			end

In short instead of moving the camera to be in front of the gun I move the gun to be in front of the camera!

1 Like

I attempted to move my AimPart’s CFrame to the HumanoidRootPart of my viewmodel (essentially the head, which is what the Camera is CFramed to) however that also doesn’t seem to work either.
I’m not very familiar with Lerping just yet and I feel as if its just an error in my code that I don’t understand or i feel that i shouldn’t have welded the AimPart to the HumanoidRootPart, but rather something else?



(Testing to see if it could aim in when holding RMB)

I’m sure my code is completely wrong, but I’m getting no errors in the output so I’m not sure where I’m going wrong with it.

2 Likes

As a test I would try directly setting the AimPart’s CFrame to the cameras CFrame so you can check if it’s a problem with how you’re getting your offset.

Looks like your viewmodel is setup a little different from how I do mine, but that shouldn’t be an issue.

image

You should try something like

if Aiming and ViewModel ~= nil then
      ViewModel.AimPart.CFrame = cam.CFrame
end

Just to ensure that your viewmodel is set up properly!

And if everything is good after that we know it’s a problem with your offset math or tweening and we can go from there.

1 Like

I just ran that code and I dont seem to be getting any errors, however the AimPart’s CFrame does not actually change.
image

2 Likes

I’d assume it’s an issue with your rig then, try adjusting the rig so that the motor6D linking AimPart and HumanoidRootPart is parented to AimPart and set Part0 to AimPart and Part1 to HumanoidRootPart and try again.

1 Like

Still no errors nor changes to the CFrame unfortunately. I must have royally screwed it up lol
image


1 Like

Haha stuff like this can be rough to figure out, I’d go ahead and comment out all the code you have in renderstepped and try only running something like:

if ViewModel ~= nil then
      ViewModel.AimPart.CFrame = camera.CFrame
end

That way we can figure out if something else is conflicting with it.

1 Like

I decided to go the extra mile and remove everything from the code that doesn’t involve creating the viewmodel or aiming, and it still seems that the AimPart doesn’t want to budge.

Heres about all of the code I have left on the local script after removing most of it

2 Likes

Alright, I went into a baseplate and made the most simple ViewModel with ADS I could think of, I’ll share the whole script for it and images of how the viewmodel is set up.

local aiming = false
local VM = game.ReplicatedStorage.ViewModel:Clone()
local cam = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local RS = game:GetService("RunService")

VM.Parent = cam

local anim0 = VM.AnimationController.Animator:FindFirstChild("Idle")
local anim1 = VM.AnimationController.Animator:FindFirstChild("AimIdle")
local anim2 = VM.AnimationController.Animator:FindFirstChild("Shoot")
local Idle = VM.AnimationController.Animator:LoadAnimation(anim0)
local AimIdle = VM.AnimationController.Animator:LoadAnimation(anim1)
local Shoot = VM.AnimationController.Animator:LoadAnimation(anim2)

mouse.Button2Down:Connect(function()
	Idle:Stop()
	AimIdle:Play()
	aiming = true
end)

mouse.Button2Up:Connect(function()
	AimIdle:Stop()
	Idle:Play()
	aiming = false
end)

mouse.Button1Down:Connect(function()
	Shoot:Play()
	VM.Effects.Sound:Play()
end)

RS.RenderStepped:Connect(function()
	if aiming == true then
		VM.AimPart.CFrame = cam.CFrame
		VM.AimPart.Color = Color3.fromRGB(255,0,0)
	else
		VM.Head.CFrame = cam.CFrame
		VM.AimPart.Color = Color3.fromRGB(0,255,0)
	end
end)

image

I figured maybe it was something with your Sway Function, but I incorporated it the same way you have yours in and didn’t have any issues with aiming, albeit I only applied it when the player is NOT aiming. I did run into issues with the ViewModel flying into space when I applied the same sway to the AimPart.

With Sway:

local aiming = false
local VM = game.ReplicatedStorage.ViewModel:Clone()
local cam = game.Workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local RS = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

local swayCFrame = CFrame.new()

VM.Parent = cam

local anim0 = VM.AnimationController.Animator:FindFirstChild("Idle")
local anim1 = VM.AnimationController.Animator:FindFirstChild("AimIdle")
local anim2 = VM.AnimationController.Animator:FindFirstChild("Shoot")
local Idle = VM.AnimationController.Animator:LoadAnimation(anim0)
local AimIdle = VM.AnimationController.Animator:LoadAnimation(anim1)
local Shoot = VM.AnimationController.Animator:LoadAnimation(anim2)

mouse.Button2Down:Connect(function()
	Idle:Stop()
	AimIdle:Play()
	aiming = true
end)

mouse.Button2Up:Connect(function()
	AimIdle:Stop()
	Idle:Play()
	aiming = false
end)

mouse.Button1Down:Connect(function()
	Shoot:Play()
	VM.Effects.Sound:Play()
end)

RS.RenderStepped:Connect(function()
	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)
	
	swayCFrame = swayCFrame:Lerp(CFrame.new(swayX,swayY,0),.3)
	
	if aiming == true then
		VM.AimPart.CFrame = cam.CFrame
		VM.AimPart.Color = Color3.fromRGB(255,0,0)
	else
		VM:SetPrimaryPartCFrame(cam.CFrame * swayCFrame)
		VM.AimPart.Color = Color3.fromRGB(0,255,0)
	end
end)

I see that you’re using a tool, I personally didn’t use a tool for this testing although it shouldn’t really change anything :man_shrugging:

If you have more issues please let me know!

1 Like

This code is seeming to help me a bit more, I put a few things from your code into my code and it look like it’s starting to go somewhere. I went in game and tested it and the viewmodel actually changes when I press down on the right mouse button. Here’s a clip of the result.


I then went back into the code to change it to add a lerp instead of just snapping it to the cframe of the camera and noticed this:

This definitely feels like a big step in the right direction, but I do want to figure out if I can make the movement smoother rather than just snapping aswell.

Update:
I put some of my old code back into the script and it looks like its working somewhat smoothly now. I think a big part of the issue might have been that I was using a different RenderStepped function for the aiming instead of putting it all in one, which is a bummer since I like to have all of my different functions separated to make them easier to fix if something goes wrong.

Here’s what I got now:

Thank you very much for your help!!

2 Likes

You’re very welcome glad I could help, if you have more questions feel free to ask away!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.