Help With RenderStepped

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!

1 Like

If it only happens because of the camera perspective, maybe it’s exactly because of that?

If you analyze that recording frame by frame, the dot does seem to update at the same time as the laser beam (for the most part), it’s just that they couldn’t agree on where the beam hits an object, possibly because the vector derived from a position used by the raycast isn’t in sync with the animation of the playermodel???

I suspect there is some difference in the laser code between first person and third person

1 Like

Seems like you were correct, and I’ve just manged to fix it! I realized I was updating the viewmodel’s position after updating the laser sight, and that’s what was causing the dot to lag behind.

Thank you so much for your help! You’ve just saved me from hours to trying to figure this out on my own.

2 Likes

Not sure if this is off-topic but this game already looks cool, what’re you making?

1 Like