Infinite RayCast?

I’m trying to figure a way to make my Raycast Infinite in a direction -
The way it works at the moment is my:
When my ship chooses a Planet to warp to, the BodyGyro will adjust accordingly - Whilst doing so, a Raycast will be fired infront of the ship to verify whether or not it’s facing the required direction / Part, if so - then the warp commenses.

However, I’ve noticed that despite my effort my warp isn’t infinite and has a limited Range.
Below is a snippet of the BodyGyro’s turning and repeating. Does anybody have any suggestion?

			RayCastType.FilterDescendantsInstances = {FacePart}
			local RayCastResult = workspace:Raycast(System["Exterior"].PrimaryPart.CFrame.p, (System["Exterior"].PrimaryPart.CFrame.LookVector * 99999999) * 250, RayCastType)
			
			local _i = 0
			repeat wait() 
				if Configuration["Status"].Value == "Idle" then break end
				if _i > 100 then
					Configuration["Status"].Value = "Idle"
					if FacePart then
						FacePart:Destroy()
					end
					return
				end
				_i = _i + 1
				print(_i)
				BodyMovers["BodyGyro"].CFrame = CFrame.new(System["Exterior"].PrimaryPart.CFrame.p, SavedDestination)
				RayCastResult = workspace:Raycast(System["Exterior"].PrimaryPart.CFrame.p, (System["Exterior"].PrimaryPart.CFrame.LookVector * 99999999) * 250, RayCastType)
			until RayCastResult

Due to engine limitations and performance reasons a single ray cast can’t be infinite.

Are you sure you need an infinitely long ray? There is surely a better way of doing this.

1 Like

Hi, I’ve experimented with different methods - Currently this is my best.

Reason for this being Planets located far beyond the Ships current Location aren’t detected when selected - That’s why I added a max of repeats so if after 100 attempts, it breaks and doesn’t warp.

Though, Ideally I want Players to be able to travel to these ‘Out of Range’ Planets.

If I could literally answer your question completely disregarding performance then I would do it like this.

local cf = System.Exterior:GetPrimaryPartCFrame()
local result = workspace:Raycast(cf.Position, cf.LookVector*5000, RayCastType) -- ray has 5k limit length
local intersection = result and result.Position or cf.Position + cf.LookVector*5000

while true do
    if not result then
        result = workspace:Raycast(intersection, Vector3.new(0, 0, -5000))
        intersection = result and result.Position or intersection + Vector3.new(0, 0, -5000)
    end
    wait(1) -- idk
end

Code is untested but should be like that. Once more tho, almost certainly a better way of doing this. Not sure 100% how your project works so won’t assume anything.

1 Like

Thanks, I’ve tried messing around with this code and cannot seem to get it to work.

Though, to note how my system works -
It uses Legacy BodyMovers and when changing the Direction of the initial Ship it is literally a BodyGyro that changes to look at the new required “Warp Point” position.

It then Raycast’s forward until said Warp Point is registered, from there it enables everything else.

EDIT://
Got it working!

EDIT2://
It’s working, just similarly to the prior method where it’s limited.