Hi, I am currently trying to find whether the player’s humanoid root part is within the boundaries of a cube. I tried
local entityLocation = -- player character humanoid root part position
local bulletInstance = -- cube part
local bulletLocation = bulletInstance.Position
local hitboxSize = bulletInstance.Size * 3 -- three is an arbitrary number which just closely matches the dimensions of the object
local function OutsideCubeBoundaries()
for _, axisInstance in pairs({"x", "y", "z"}) do
if (math.abs(entityLocation[axisInstance] - bulletLocation[axisInstance]) > hitboxSize[axisInstance]) then
return true
end
end
end
if not OutsideCubeBoundaries() then
print("in cube")
end
However, it doesn’t work accurately if the cube is rotated, so I am looking for a new method. How can I detect if the player is within the bounds of a rectangle rotated at, for example, 45 degrees? I’d like to not use raycasting if possible and I cannot use the Touched connection.
Hi, this would probably work the best for my needs if get touching parts worked differently, but since it uses a touch interest like the touched connection, it can be exploited by the client just by deleting it.
I have kind of thought of a method in which I can get all of the vertices of a cube by adding or subtracting three axes of its size divided by two multiplied by the cube’s CFrame, but I am still unsure how I could use this to see whether another position is inside the boundaries of eight points.
You can try this instead. Let me know if it works:
local entity = -- player character humanoid root part
local bullet = -- cube part
local function OutsideCubeBoundaries()
if (table.find(workspace:GetPartsInPart(entity), bullet)) then
return true
end
end
if not OutsideCubeBoundaries() then
print("in cube")
end
If I’m not mistaken, the OutsideCubeBoundaries() function returns a boolean which depicts if the player is INSIDE the cube. You have a not operator in the if statement, this would only run if the player was OUTSIDE of the cube.
Something like this would achieve what the OP is requesting:
local function InsideCubeBoundaries(player: Player, cube: BasePart): boolean
local Character = player.Character
if (Character) then
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
if (HumanoidRootPart) then
return not not table.find(workspace:GetPartsInPart(cube), HumanoidRootPart)
end
end
return false
end
-- Usage: InsideCubeBoundaries(player: The player to do the check on, cube: The part in which to check if the player is inside)
Hi, the GetPartsInPart method looks like it would work very well for my situation. However, I am creating the bullet on a local script and moving its hitbox on the server. Because of this, I don’t have the part available and I would have to create and tween an invisible object made on the server just to get this to work. I did find the GetPartBoundsInBox function while looking at the documentation of GetPartsInPart which I think will work if there isn’t another way to do so using math, so @daisytheghostchild98 's response has still been helpful, thanks.
I think the GetPartBoundsInBox function will work the best and I will use that if there is no better solution. It would be a little bit more efficient to test if just one object is in a bullet rather than finding all of the objects in it and then sorting through that, so is there a formula or some way that I can use to detect if a position is inside the eight vertices of a cube, if I already have all of the positions?
most ppl use raycast to determine if a player or object is in the line of the bullet and do damage by that if i am understanding what your trying to do