greetings,
it is notsad again, and this time i appear to be struggling with a cframe issue!
i’m trying to make an ADS system by setting the position of an aimpart inside the gun to the cframe of the camera (see code below)
truthfully speaking, i have no idea what i’m doing and i am very bad at this
but the process should look like this:
- calculate position every frame to account for viewmodel positions changing (etc.)
- lerp to that position
- the lerping ‘stops’ when the aimPart reaches the camera
the aimpart is slightly behind the (non-existent) back-sights on the AK47
if this wasn’t obvious, this is NOT what aiming down sights looks like
my code looks like this
Aim = function(gun: Tool, viewmodel: Model)
local fractured = utility.GetGameplayValue('fractured')
if fractured.Value then return end
local effectParts: Folder = viewmodel.EffectParts
local aimPart: BasePart = effectParts.AimPart
local aimSpeedTweak = utility.GetGameplayValue('aimSpeedTweak')
local speed = 1 / (vanillaAimSpeed / aimSpeedTweak.Value)
local originalCFrame = viewmodel:GetPivot()
rService:UnbindFromRenderStep('WeldViewmodel')
rService:BindToRenderStep('Aim', Enum.RenderPriority.Camera.Value, function(deltaTime)
local aimOffset = aimPart.CFrame:ToObjectSpace(viewmodel.PrimaryPart.CFrame)
local targetPrimaryCFrame = camera.CFrame * aimOffset:Inverse()
local alpha = math.clamp(deltaTime * speed, 0, 1)
local currentCFrame = viewmodel:GetPivot()
local newCFrame = currentCFrame:Lerp(targetPrimaryCFrame, alpha)
viewmodel:PivotTo(newCFrame)
end)
aim:FireServer(true)
mouse.Button2Up:Once(function()
rService:UnbindFromRenderStep('Aim')
rService:UnbindFromRenderStep('AimReturn')
aim:FireServer(false)
local returnSpeed = 1 / vanillaAimSpeed
rService:BindToRenderStep('AimReturn', Enum.RenderPriority.Camera.Value, function(deltaTime)
local currentCFrame = viewmodel:GetPivot()
local alpha = math.clamp(deltaTime * returnSpeed, 0, 1)
local newCFrame = currentCFrame:Lerp(originalCFrame, alpha)
viewmodel:PivotTo(newCFrame)
if (newCFrame.Position - originalCFrame.Position).Magnitude < 0.1 then
viewmodel:PivotTo(originalCFrame)
rService:UnbindFromRenderStep('AimReturn')
utility.ReweldViewmodel(viewmodel, camera)
end
end)
end)
end,
please help