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
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)
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.
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
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: