Would it be better to use a raycast system instead of a stud radius?

Writing some car ai. Rewriting the detection system as I have a fully working race car system down now. Was wondering would it be better to use rays or a radius?

The game is going to be some what large, be multiplayer and have multiple ai instance clones like it. Please keep that in mind.

The code runs the same as other topics I have posted here before although I have slightly changed the functionality but just think of it as being the same for ease of mind and so I don’t have to explain something that has 0 impact on this.

Yes it runs in a loop, consistently as all the main functionality happens within the idle function. Think of it as being the main/entry point of the ai.

post:

https://devforum.roblox.com/t/about-memory-optimisation-and-need-some-tipsguidance/3316844
NO THIS IS NOT ME ASKING FOR PERFORMANCE HELP JUST AS A REF FOR THE SERVER SCRIPT


Code:

        local originPosition = new.seat.CFrame.Position
		local radius = 30 -- STUDS DISTANCE

		local findNearbyInstances = function(position, radius)

			local PlayerCars = game:GetService("CollectionService"):GetTagged("Car")
			local nearbyInstances = {}

			if #PlayerCars > 0 then
				
				for _, WildCar: Model in ipairs(PlayerCars) do
					
					if WildCar:GetAttribute("PlayerCar") ~= nil and WildCar:GetAttribute("PlayerCar") == true then
						
						if #WildCar:GetChildren() > 0 then
							
							local Wseat = WildCar:WaitForChild("DriverSeat")
							
							if Wseat ~= nil then

								local distance = (Wseat.CFrame.Position - position).magnitude

								if distance <= radius then
									table.insert(nearbyInstances, WildCar)
								end

							end							
							
						end
						
					end
					
					--[[if part:IsA("Model") and #part:GetChildren() > 0 and part:IsDescendantOf(script.Parent) == false then

						-- i need to rewrite this part. it is old and outdated for what i am doing.
						if part:FindFirstChildOfClass("VehicleSeat") then

							local distance = (part:FindFirstChildOfClass("VehicleSeat").CFrame.Position - position).magnitude

							if distance <= radius then
								table.insert(nearbyInstances, part)
							end

						end								

					end]]
				end

			end
			

			return nearbyInstances
			
		end

Use a radius, raycasts are only useful for single-line detection.

Also, raycasts + the “radius” aren’t that laggy. So don’t worry.

1 Like

Fair enough there is a few ways could go about it but I suppose radius is best.