Raycasting through a portal not resulting in a centre shot

I’m having issues where shooting through a portal isn’t resulting your shot actually being correct, and closer you get, the more innacurate it gets. If your FirePoint (the tip of the gun) is clipping into a wall/portal, then I shoot from the camera, and it works perfectly, however I obviously need the bullet to come from the gun in most cases.

local MousePos = UserInputService:GetMouseLocation() or Mouse
	
if self.Mag > 0 then -- Can fire
	local UnitRay = Camera:ViewportPointToRay(
		MousePos.X,
		MousePos.Y
	)
	
	local FirePoint = UnitRay.Origin
	
	if self.WeaponFolder:FindFirstChild("Projectile") then
		FirePoint = self.FirePoint.WorldPosition
		
		local WallRaycast = workspace:Raycast(
			UnitRay.Origin,
			FirePoint - UnitRay.Origin,
			CastParams
		)
		if WallRaycast then -- Infront of wall
			FirePoint = UnitRay.Origin
		end
	end
	
	local Direction
	
	local HitRaycast = workspace:Raycast(
		UnitRay.Origin,
		UnitRay.Direction * 1000,
		CastParams
	)
	
	if HitRaycast then
		Direction = (HitRaycast.Position - FirePoint).Unit
	end
	
	if not Direction then
		Direction = Camera.CFrame.LookVector * 1000
    end
end
2 Likes

If the ray hits the portal you just need the position related to the portal position and then start another ray.
Example:
``
Cf = Camera.CFrame
start = Cf.Position
local ray = workspace:Raycast(start,end,parameters)
If ray then

if ray.Instance.Name == “screenportal” then
local curcf = Cf
Curcf.Position = ray.Position
Local related = ray.Instance.CFrame:ToObjectSpace( urcf)
local start1 = OthePortal.CFrame:ToWorldSpace(related)
–raycast from the start1.Position

end

end

``
If you want i can do a model for you to help on that(will be able to do it tomorrow)

Ummmmm this is a 1 year old thread… its probably solved already.

well, if it’s solved the thread creator needs to mark it as solved

they forgor obv :skull: now let’s not necropost since this is a dead topic