Trying to make a part adapt to terrain

im trying to make a building system for my game, i have the part moving already, however i cant figure out how to get the rotation working, i want it to adapt to the terrain (edit: it is actual terrain and not parts) and rotate,
here is the video: (nevermind i cant upload videos for some dang reason)
i cant find anything about this on devforum or youtube, please help :slight_smile:
code:

local mouse = game.Players.LocalPlayer:GetMouse()
local part = Instance.new("Part")
part.Parent = workspace
part.Anchored = true
part.CanCollide = false
part.CanTouch = false
part.CanQuery = false
mouse.Move:Connect(function()
	part.Position = mouse.Hit.Position
end)

You could use Raycasting & RaycastResult.Normal to position & orientate the part with respect to terrain

can you explain? im confused
idk about raycasts

Something like this would work.

Essentially what you’re doing is sending a Raycast out of the front and looking for collisions, if one happens, it rotates 10 degrees up, and goes again, and repeats until a clear path is found.

If you want it to turn back eventually you could also send one out the side.

ezgif.com-speed

1 Like

i edited the script to make it take into account terrain angle

local mouse = game.Players.LocalPlayer:GetMouse()
local part = Instance.new("Part")
part.Parent = workspace
part.Anchored = true
part.CanCollide = false
part.CanTouch = false
part.CanQuery = false
local rayParams = RaycastParams.new()
rayParams.FilterDescendantsInstances = {part}
rayParams.FilterType = Enum.RaycastFilterType.Exclude
mouse.Move:Connect(function()
	local camPos = workspace.CurrentCamera.CFrame.Position
	local result = workspace:Raycast(camPos, (mouse.Hit.Position-camPos)*10, rayParams)
	if result then
		local normal = result.Normal
		local frame = CFrame.fromMatrix(mouse.Hit.Position, normal:Cross((mouse.Hit.Position-camPos).Unit), normal)
		part.CFrame = frame
	end
end)

you might want to edit the second/fourth parameter of the cframe.frommatrix depending on how you want the part oriented on the other axis

I’m not the best at explaining things, so I’m just gonna link tutorials.

First though, a prerequisite for raycasting is knowing how to use CFrame, which allows you to position & orientate a part: https://youtu.be/8Ycrq3yp-ME?si=FnBurxPQu_sLOcNk

& here’s a good raycast tutorial that I found helpful personally: https://www.youtube.com/watch?v=YipkISTwGlc&t=535s

The reason raycasting helps in this situation is because with raycasting, you’re able to find out the direction which the surface of something is facing, whether that be a BasePart or Terrain.

It looks like @jaymeyzy has got you covered with a solution, but I’ll also just post mine because I already worked on it:

Video Demo: https://youtu.be/pY1Uanufyj0?si=joW1wJdMjVeVZS-i

Code:

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