"Dual Render" Scope System

I just realized I mispoke… If the ray doesn’t hit the player than you can count the scope as invisible. There are obviously some flaws in this however.

Using this method of checking for clipping via raycasts, this issue is mostly avoided!

Whenever the tip of the Scope gets clipped, it goes dark!

DevForum kept erroring when I tried uploading in 1080p60 so enjoy this gross built in recorder version

--Altered code for this change

--Only update camera CFrame when scope CameraPart moves
GetPropertyChangedSignal(CameraPart, "CFrame"):Connect(function()
	Camera.CFrame			= CameraPart.CFrame 
	local vector = WorldToScreenPoint(PlrCamera, ScreenPart.CFrame*v3(0,0,-ScreenPart.Size.Z/2))
	if vector.X ~= RenderFrame.Position.X.Offset then
		RenderFrame.Position	= udim2(0,vector.X,0,vector.Y)
		RenderFrame.Size		= GetObjectScreenSize(ScreenPart)
		RenderFrame.Rotation	= ScreenPart.Orientation.Z
	end
	--Check for clipping
	local ray = Ray.new(
		PlrCamera.CFrame.Position,
		((CameraPart.CFrame.Position+(CameraPart.CFrame.LookVector*5)) - PlrCamera.CFrame.Position).unit * (PlrCamera.CFrame.Position - CameraPart.Position).magnitude
	)
	local hit = workspace:FindPartOnRayWithIgnoreList(ray, {Character,GunModel.Parent.Parent})
	if hit then
		ViewPort.Visible = false
	else
		ViewPort.Visible = true
	end
end)
2 Likes

Great fix! This actually wasn’t what I was pointing out. I was showing that even though the scope isn’t visible when you clip the gun through walls the ViewportFrame is.

Ohhh

Well, we can apply this same method the the ScreenPart instead of the CameraPart!

If the ScreenPart isn’t visible, then make the entire scope GUI not visible!

Ahhhh, never make the gun CanCollide. You will fling 99.999999% of the time.

Yup! That’s why I didn’t go with that!

Decided to update my example place with some nicer touches.

  • Scope fades out when unaimed (also made it only update when not faded, so it doesn’t eat CPU when unaimed)
  • Blur effect fades rather than appears
  • Added dirty glass texture over the scope
  • Added lighting effects for fun
External Media
3 Likes

Would you have a download for those updated touches?

Some are specific to my FPS system, so no.

To do the performance saving when unaimed, it’s a simple if statement.

PartUpdater = Heartbeat:Connect(function()
	if ViewPort.Visible and ViewPort.ImageTransparency~=1 then

I get an error involving a Primary Part when I try to use the script as is. The error is as follows:

RunService:fireRenderStepEarlyFunctions unexpected error while invoking callback: Model:SetPrimaryCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this.

This is the only error I get. I tried setting the guns primary part in the script but I had no success.

This is super neat. If ViewportFrames could be non-square, it might be possible to create a Depth of Field effect with this. Excellent work!

2 Likes

I think I have a way to be able to optimise it a bit more.
If you only render/update items then are in view.
I’m pretty sure this is possible, but not 100%.
It is a great read and I will be using this and a setting for people.

Could this be transformed into a magnifying glass?

Yes it could probably work with a magnifying glass, but it would not be suitable for a real game environment like they said.

1 Like

I keep getting this error,
09:44:20.165 Model:SetPrimaryPartCFrame() failed because no PrimaryPart has been set, or the PrimaryPart no longer exists. Please set Model.PrimaryPart before using this. - Client - ViewportCameraController:47

I don’t know how to fix it :pensive:

This error is telling you that there is no set primary part for the model. Make sure to check if the model does have a primary part by looking at its properties, and if so then make sure nothing is setting it to nil. It may be getting destroyed as well.

How would I set the correct primary part? I know how to set one but I don’t know which part to set it as…

hmm I downloaded the world and tried it. I see what you mean now! For some reason the cloned model of the gun does not have a primary part set, so we will have to edit the script a little to make it so it does. Sure enough, it worked! All you have to do is go into: StarterGui > ScreenGui > DualRender > and open the script called “ViewportCameraController.” Make a line above line 47 and copy and paste this into it:

GunModel.PrimaryPart = GunModel.AimPart

It should be working now! Let me know if you have more trouble.

3 Likes

I’ll check it tomorrow. Thanks for your help!

i wonder if the demo works properly on mobile. i can launch the demo world just fine, just can’t aim down the scope.