So what i'm trying to create is a function that spits out a random point on the top surface of a brick, regardless of the brick's rotation, position, or size, which is why I am using directions perpendicular to the faces (lookVector, rightVector, etc)
However, my problem with it is that when the brick is rotated 90* of -90*, the random point always becomes the center of the face.
I'm probably doing something stupid, but here's my code:
function getRandomPointOnTopSurface(Part)
local Point = nil
if Part.Shape == Enum.PartType.Block then
local minLook = Part.CFrame - (Part.CFrame.lookVector * Part.Size.Z/2)
local maxLook = Part.CFrame + (Part.CFrame.lookVector * Part.Size.Z/2)
local minRight = Part.CFrame - (Part.CFrame.rightVector * Part.Size.X/2)
local maxRight = Part.CFrame + (Part.CFrame.rightVector * Part.Size.X/2)
if minLook.z > maxLook.z then -- switches values so the min isn't greater than the max in math.random()
maxLook = (-Part.CFrame.lookVector * Part.Size.Z/2)
minLook = (Part.CFrame.lookVector * Part.Size.Z/2)
end
if minRight.x > maxRight.x then
maxRight = Part.CFrame + (-Part.CFrame.rightVector * Part.Size.X/2)
minRight = Part.CFrame + (Part.CFrame.rightVector * Part.Size.X/2)
end
Point = Vector3.new(math.random(minRight.x, maxRight.x), (Part.CFrame + (Part.CFrame.upVector * Part.Size.Y/2)).y, math.random(minLook.z, maxLook.z))
print(Point)
end
return Point
end
getRandomPointOnTopSurface(workspace.Grass)
Please note I'm not asking for anyone to completely redo my code; I just want to know the flaw with the function -- I can fix it from there
