I’m making a building game and intend to implement plots. In order to do this, I need to check if a Vector3 submitted by the player is within a certain cube-shaped area.
Here is a function which determines whether a Vector3 position is in an area.
local function IsPositionInPart(Position, Part) -- Vector3,Instance local RelPosition = Part.CFrame:PointToObjectSpace(Position) if math.abs(RelPosition.X) <= Part.Size.X/2 and math.abs(RelPosition.Y) <= Part.Size.Y/2 and math.abs(RelPosition.Z) <= Part.Size.Z/2 then return true end return false end
This is how you use it…
local Part = Instance.new("Part") Part.Size = Vector3.new(10, 10, 10) Part.Position = Vector3.new(0, 0, 0) local inputPosition = Vector3.new(9, 0, 0) local inArea = IsPositionInPart(inputPosition, Part) Part:Destroy() -- Optional, you could also resort to making a table for the Position and Size warn("inArea = " .. tostring(inArea))
The output should look like this
> inArea = true
You could use Raycasting for this where you can Whitelist the cubed area using:
And that will be the only part where the raycast will register and you’ll be able to place the block.
Thank you so much for the help!