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?
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?
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.
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.
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.
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.
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.
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)
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
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.