Spawn a block that matches the angle of the part clicked?

I want to make a drawing game where people can draw on buildings and stuff.
It will be easier to explain for me through images since English isn’t my first language.

What I did already:

What I’m trying to do (match the part that spawns match the angle on the part where the player mouse hovers. Also I want the front face facing up):
image


This is my script:

local userInputService = game:GetService("UserInputService")
local players = game:GetService("Players")
local runService = game:GetService("RunService")


local localPlayers = players.LocalPlayer

local mouse = localPlayers:GetMouse()

local on = false

userInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		on = true
		while on do --// i know i can use renderstepped but im testing something.
			local p = Instance.new("Part")
			p.Anchored = true
			p.CanCollide = false
			p.CanQuery = false
			p.CanTouch = true
			p.Size = Vector3.new(0.5, 0.5, 0.5)
			p.CFrame = mouse.Hit
			p.Parent = workspace
			p.Color = Color3.fromRGB(255, 0, 0)
			task.wait(1)
		end
	end
end)

userInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		on = false
	end
end)

What I would do is get the Part that the mouse is hovering, get the Orientation Property and copy it in your Part

1 Like

Well, on wedges that wouldn’t work, good idea though.

An idea would be to put a Part on the Wedge and get it’s Orientation

For wedges that would be way more complicated.

It might not work, but finding the hypotenuse using the pythagorean theorem (a² + b² = c²) and applying that number to the Y axis of the orientation could work.
It very well might not, though. It’s a bit of a stretch :joy:

I was thinking the same thing. I’ll try it. Thanks.

1 Like

Another easy is because the Wedge Orientation changes only with the X and Y axis (correct me if I’m wrong) you could check this and do some calculations that (hopefully) are easier the Pythagorean Theorem :rofl::rofl::rofl:

1 Like

That might work, but I’m not completely sure.

1 Like

Yes, there’s only 4194304 possible combinations, with only 360 possible results, a cakewalk, literally :rofl::rofl::rofl:

1 Like

only possible with raycasting, using the normal of the ray you can calculate the correct rotation

local ray = workspace:Raycast(workspace.CurrentCamera.CFrame.Position,mouse.Hit.LookVector*1000)
local normal = (ray and ray.Normal) or Vector3.new(0,0,0)
p.CFrame = CFrame.new(mouse.Hit.Position,mouse.Hit.Position+normal)
1 Like

This works, but how I do it so it works on the right surface?

Yeah. Surely extremely easy!


In all seriousness, using the Pythagorean theorem would be much easier. Could be done very simply with something like this:

local wedge = workspace:WaitForChild("Wedge")

local function getHypotenuse(part: WedgePart) : number
	local aSquared = part.Size.Z * part.Size.Z
	local bSquared = part.Size.Y * part.Size.Y
	
	local cSquared = aSquared + bSquared
	
	local c = math.sqrt(cSquared)
	
	return c
end

local hypotenuse = getHypotenuse(wedge)
print(hypotenuse)

And you can even vary which one you need if you ever needed both or a specific one. You could just return both c² and c, then create a variable to follow that:

local wedge = workspace:WaitForChild("Wedge")

local function getHypotenuse(part: WedgePart) : number
	local aSquared = part.Size.Z * part.Size.Z
	local bSquared = part.Size.Y * part.Size.Y
	
	local cSquared = aSquared + bSquared
	
	local c = math.sqrt(cSquared)
	
	return cSquared, c
end

local cSquared, hypotenuse = getHypotenuse(wedge)
print(cSquared, hypotenuse)
1 Like

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