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
Well, the first thing I would do is cache the value of "target.Position - target.Size/2."
Also, raycasting and getting the normal would also be useful.