How to rotate rays?

Hello, me and @sasial are trying to make a node generation that train will follow, we’re using node generation system by prebuilt nodes, and using rays to connect nodes. Our problem occurs when we try to connect nodes in curves using rays. The ray will simply continue and hit parts that we don’t want to hit.


Is there any way to rotate it, so we can make the curve?

Also here is our code:

function doray(i)
	if i:IsA("Part") then
		print("i is a part")
	end
	print(i.Name)
	local o = Vector3.new(i.Orientation.Y --[[/2--]],0,300) 
	print(o)
	local ray = Ray.new(i.Position,o)
	local hit, position = workspace:FindPartOnRayWithIgnoreList(ray,{i})
	if hit then
		print("Found somethin'")
		print(hit.Name,position)
		print(hit)
		if hit.BrickColor == BrickColor.new("Maroon") then
			print("Node found. (:")
			i.BrickColor = BrickColor.new("Lime green")
			hit.BrickColor = BrickColor.new("New Yeller")
			doray(hit)
		else
			print("Node not found")
			if not hit:FindFirstChild("SelectionBox") then
				print('new sb')
				local sb = Instance.new("SelectionBox",hit)
				sb.LineThickness = 1
				sb.Color3 = Color3.new(0,255,0)
				sb.Adornee = hit
			end
		end
	else
		print("Hit nothin'")
	end
end
doray(workspace.Tracks.Start.PrimaryPart)

Thank you for your help, it is much appreciated.

1 Like

I am not quite understanding the question. It sounds like you are asking if you can make a curved ray, which from my understanding is not possible. You can draw many short, angled rays though, which would essentially be a curved line.

Can you please explain in a bit more detail what is happening, what you want to happen, and what you have tried to make that happen? Thanks!

Yes sure, we don’t want to make one large ray that will be from the start of the curve till the end of the curve, we want to use small ones. We just have problems of rotating it, because we both are really new to raycasting. Thank you for your response!

EDIT: Clarification

Is the function doray run only once?

No.
It is creating a ray to find a node.

doray(workspace.Tracks.Start.PrimaryPart)

It starts by finding the ray for the next node after the starting node.
Then, it’ll run the function again for that node it found.
Etc.

Do you currently have nodes between each point a ray should start and end? Or will it need to calculate the starting/stopping points on it’s own while going around corners?

Yes. I do.
The issue is getting the ray to rotate.

Making a ray “rotate” is as simple as changing the direction component when creating the ray. You cannot alter the ray’s direction once it is created (from my understanding).

I get that.
And I’m changing it.
It’s just not working.

	local o = Vector3.new(i.Orientation.Y --[[/2--]],0,300) 

Not working.

What is ‘o’ and why would that change anything? Can you show me more of the script? That line doesn’t do anything on its own so I can’t help you without seeing what else it does.

The whole script’s in post #1.
I’m just using these lines to refer to the code.

Sorry, I missed that recursion earlier. The direction is not related to orientation, its the position point that the ray aims at

Oh, thanks. Forgive me for missing that.

I think you should have both nodes including in creating the ray.

local node1pos, node2pos = node1.Position, node2.Position -- starting node, finishing node (then update the finishing node to be the new starting node, and the next node to be the new finishing node)
local ray = Ray.new(node1.Position, (node2.Position-node1.Position)) -- subtract the first node's position so that it only travels the difference in points (rays dont go from point A to point B, they from point A to point B+A, so if you subtract pointA then you get pointB).

We don’t know the position of the next node.
That is why we are trying to use the ray to find out.

You could also use trigonometry, something like

o=i.Position+Vector3.new((i.Size.X/2)cos(i.Orientation.Y),0,(i.Size.X/2)sin(i.Orientation.Y)),

however I made this equation assuming a certain orientation. So try this out altering X and Z respectively

1 Like

Any ideas on making it a bit easier, we’re not that advanced in math.

Yeah sorry, could you tell me the size of the node. It should be based off the long end, so If I knew which axis that was I could have something for ya.

1,1.2, however long the node is.

it’s not clear (to me) what you mean by node. Is it each track segment or the bigger green box? Are you trying to do the picture above or below?

The picture picture at the top is an error, because we weren’t able to change rotation of ray, the picture at the bottom is exactly what we want to do. Imagine nodes like dots that are in the middle line (the color is maroon), all nodes are placed here.

all of them are located in position that arrows are pointing at.