Raycast range problem

hi im ctg i need help
im making this type range for my td game


the video is in somewhere i found*

local ray = Ray.new(Tower.Range.Position, Vector3.new(rangeVal,Tower.Range.Position.Y,rangeVal))
local PartFound, Pos = workspace:FindPartOnRay(ray)
	
if PartFound then
	print("Target Near")
end

the script didnt print part in range except it touch a part
also i wanna ask how i can make it union like in the video?

3 Likes

It isn’t a union. Those are many many smaller parts that are close to each other and they build a circle. It is hard to visualize. You can see in this video that it look like a union but it just a lot of smaller parts.

In the video he probably makes 360 rayscasts and that because a full rotation around the part is 360° (full circle).

So he makes a raycast from the part in the direction the part is looking, then he looks how far the raycast has come for example 20 studs and then creates a part that is 20 studs long and sets its position to the rays position. (You basically visualize the raycast with a part)

After that you rotate the part 1° and repeat what I said before (I believe in the third paragraph) and when you do that 360 times and rotate the part 1° and make a ray and visualize it with a part you can get that effect.

I am sorry if is hard to understand, if you don’t understand something message me as soon as possible and I will try to help you by making a video or something.

2 Likes

You can try this:

local observerPart = workspace.Object -- object that will raycast around itself
local ignoredObjects = {} -- objects that ray ignores

local range = 5 -- range of ray
local raysAmount = 360 -- amount of rays that will be casted around the object

-- :: Create parameters for raycasting

local usedParams = RaycastParams.new()
usedParams.FilterType = Enum.RaycastFilterType.Blacklist
usedParams.FilterDescendantsInstances = ignoredObjects

local pi = math.pi

local function doRaycast(degree)
	
	-- :: Calculate direction based on angle
	
	local degreeToRadian = (degree / raysAmount) * pi * 2
	local direction = Vector3.new(math.sin(degreeToRadian), 0, math.cos(degreeToRadian)) * range
	
	-- :: Return raycast results
	
	return workspace:Raycast(observerPart.Position, direction, usedParams)

end

local function instanceFound(result)

	local instanceHit = result.Instance -- Instance that got hit
	
	-- :: Do something with that instance

end

-- :: Constant loop, 60 times per second

while task.wait() do
	for degree = 1, raysAmount do
		local result = doRaycast(degree)

		if result then
			instanceFound(result)
		end
	end
end

Demo :

  • Red part: result.Position
  • Cyan part: instanceHit.Position + CFrame.new(instanceHit.Position,observerPart.Position).LookVector

9a3175e127e034241753881261a5d9cd

@danipoplpl explained how to do the visual part.

4 Likes

i ask the owner video again and he said att and part but thank for tell me

i can understand it, let me try


nice thanks
also thank @danipoplpl too

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.