I’ve been working on a laser sight for a gun, and I’ve ran into an issue with the laser dot not updating fast enough in first person. The dot seems to lag behind and not update fast enough while running it from the viewmodel, but works properly when in third person.
The code inside of the RenderStepped:
if inFirstPerson() then
updateViewModel(true)
currentBodyAttach = currentGunClass.ViewModel[currentGunClass.Name.."Model"].BodyAttach
-- main code below
if currentGunClass.UnderbarrelAttachment then
local enabled = currentGunClass.UnderbarrelAttachment.Enabled
if enabled then
currentGunClass.UnderbarrelAttachment.Update(currentGunClass.ViewModelAttachments["Underbarrel"], true)
currentGunClass.UnderbarrelAttachment.Update(currentGunClass.GunAttachments["Underbarrel"], false)
else
currentGunClass.UnderbarrelAttachment.Update(currentGunClass.ViewModelAttachments["Underbarrel"], false)
currentGunClass.UnderbarrelAttachment.Update(currentGunClass.GunAttachments["Underbarrel"], false)
end
end
else
updateViewModel(false)
currentBodyAttach = currentGunClass.GunModel.BodyAttach
-- main code below
if currentGunClass.UnderbarrelAttachment then
local enabled = currentGunClass.UnderbarrelAttachment.Enabled
if enabled then
currentGunClass.UnderbarrelAttachment.Update(currentGunClass.GunAttachments["Underbarrel"], true)
currentGunClass.UnderbarrelAttachment.Update(currentGunClass.ViewModelAttachments["Underbarrel"], false)
else
currentGunClass.UnderbarrelAttachment.Update(currentGunClass.GunAttachments["Underbarrel"], false)
currentGunClass.UnderbarrelAttachment.Update(currentGunClass.ViewModelAttachments["Underbarrel"], false)
end
end
end
The Update Function (excuse how messy it is):
local laser = attachmentModel.LaserSight:FindFirstChild("Laser")
local laserOrigin = attachmentModel.LaserSight:FindFirstChild("LaserOrigin")
local laserEnd = attachmentModel.LaserSight:FindFirstChild("LaserEnd")
if enabled then
laser.Enabled = true
attachmentModel.LaserDot.Transparency = 0
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Blacklist
rayParams.FilterDescendantsInstances = { workspace.RaycastIgnore, workspace.CurrentCamera, game.Players.LocalPlayer.Character }
local ray = workspace:Raycast(
laserOrigin.WorldPosition,
laserOrigin.WorldCFrame.LookVector * 1000,
rayParams
)
local endPosition
if ray then
endPosition = ray.Position
-- attachmentModel.LaserDot.Position = ray.Position
attachmentModel.LaserDot.CFrame = CFrame.new(ray.Position, ray.Position + ray.Normal) * CFrame.Angles(0, math.rad(90), 0)
laserEnd.WorldPosition = attachmentModel.LaserDot.Position
else
endPosition = laserOrigin.WorldCFrame.LookVector * 1000
end
else
attachmentModel.LaserDot.Transparency = 1
laser.Enabled = false
end
The Laser In First Person:
The Laser In Third Person:
If anyone could help, that would be really appreciated! Thank you for reading!