Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Making this function shorter using math?

I'm creating a tool that places blocks, and so I made this function that returns a Vector3 in which it will later help the tool to place blocks on the sides or on top of other blocks.

This function will return a Vector3, and each parameter of this vector will be either 1, -1 or 0. Though I have been thinking this function is way to big, and by looking at how this function behaves, I thought I could make this function smaller by using some math. I'm rather bad in math and I would like to know If a can make this function shorter using math and how could I do it.

Script:

function TargetFaceVector(target, mousePos)
	--Create the vector
	local vector = Vector3.new(0,0,0)
	
	--The mouse is pointing on the right face
	if mousePos.X == target.Position.X + target.Size.X / 2 then
		vector = vector + Vector3.new(1,0,0)
	end
	
	--The mouse is pointing on the left face
	if mousePos.X == target.Position.X - target.Size.X / 2 then
		vector = vector + Vector3.new(-1,0,0)
	end
	
	--The mouse is pointing on the top face
	if mousePos.Y == target.Position.Y + target.Size.Y / 2 then
		vector = vector + Vector3.new(0,1,0)
	end
	
	--The mouse is pointing on the bottom face
	if mousePos.Y == target.Position.Y - target.Size.Y / 2 then
		vector = vector + Vector3.new(0,-1,0)
	end
	
	--The mouse is pointing on the front face
	if mousePos.Z == target.Position.Z + target.Size.Z / 2 then
		vector = vector + Vector3.new(0,0,1)
	end
	
	--The mouse is pointing on the back face
	if mousePos.Z == target.Position.Z - target.Size.Z / 2 then
		vector = vector + Vector3.new(0,0,-1)
	end
	
	--Return the face vector
	return vector
end

2 comments

1

Well, the first thing I would do is cache the value of "target.Position - target.Size/2."

1

Also, raycasting and getting the normal would also be useful.

1 answer

Assuming mousePos is always a position along the surface...

if your part was always at (0, 0, 0) and sized (2, 2, 2), then the mouse position would always be something like (0.23, -1, 0.76) or (1, 0.98, 0.34) -- (at least) one of the coordinates is 1 or -1 and the rest are not.

You want to keep the 1s and -1s but map the rest to 0:

function keep1s(n)
	if n == 1 or n == -1 then
		return n
	end
	return 0
end

Then just apply this to all three coordinates:

function targetFaceVector(target, mousePos)
	local onSurface = mousePos

	return Vector3.new(keep1s(onSurface.x), keep1s(onSurface.y), keep1s(onSurface.z))
end

What if your part is not sized (2, 2, 2) and at (0, 0, 0)? No problem; first, offset the mouse by the position to effectively "place" the part at (0, 0, 0), then divide by half the size to effectively size it to (2, 2, 2):

function targetFaceVector(target, mousePos)
	local onSurface = (mousePos - target.Position) / (target.Size / 2)

This discussion is preserved as it was. New answers, comments, and votes are closed.