Checking corners of a Part?

Hi, I’m working on a Hitbox system of my own and am looking to advance it. Currently, it works fine by using the green example below: It creates a Ray between the Top & Bottom attachment. However, I’m attempting to further this using the Red example, in all corners - Therefore creating four Rays as opposed to the one.

Now, I know how I can do this using Attachments themselves, however I’m looking to remove the use of Attachments themselves and get the required information solely using the Size of the Part.

This is currently how it functions:

local Origin = Tool["HitBox"]["AT0"].WorldPosition
local Direction1 = Tool["HitBox"]["AT1"].WorldPosition
local Direction2 = (Origin - Direction1).Unit * 2
    				
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {Player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local raycastResult = workspace:Raycast(Origin, Direction2 * ((Origin - Direction1).Magnitude), raycastParams)
4 Likes

Hi, I’m not sure what you’re trying to do, but if you just want to tell is a point is inside a part you can use something like this:

function PointInPart(point, part)
	
	local p = part.CFrame:PointToObjectSpace(point)
	p = p * 2
	
	if (p.x < -part.Size.x  or p.x > part.Size.x or
		p.y < -part.Size.y  or p.y > part.Size.y or
		p.z < -part.Size.z  or p.z > part.Size.z) then
		return false
	end
	return true
	
end
1 Like

Sorry if it wasn’t clear enough!

Left is currently how it works, creating a single Ray from the bottom of the Part to the top of the Part directly through the middle - This uses Attachments to determine the positions required.

Right is what I’m attempting to achieve - Four rays, each from the bottom corner to the top corner. However, I want to remove the use of attachments to determine where the Position should be and do this solely using Maths & Size.

Okay!

You can use part.CFrame:PointToWorldSpace(point) to do just that.

local topCorner = Vector3.new(part.size.x / 2, part.size.y /2 , part.size.z / 2) 
local cornerOfPart = part.CFrame:PointToWorldSpace(topCorner)

(the other corners would be like Vector3.new(-size.x /2, size.y/2, size.z/2) etc )

How could I implement this into a RayCasting function? so that in a single Heartbeat it’d be like:

BottomLeft → TopLeft
BottomRight → TopRight
BottomFront → TopFront
BottomBack → TopBack

You can get 1 corner by using:

local Size = Part.Size
local CornerPos = (Part.CFrame * CFrame.new(Size .X/2, Size .Y/2 , Size .Z/2)).Position

That will give you one corner. Now, you can use a table that indicates where each corner is:

local Vertices = {
	{1, 1, -1},  --v1 - top front right
	{1, -1, -1}, --v2 - bottom front right
	{-1, -1, -1},--v3 - bottom front left
	{-1, 1, -1}, --v4 - top front left
	
	{1, 1, 1},  --v5 - top back right
	{1, -1, 1}, --v6 - bottom back right
	{-1, -1, 1},--v7 - bottom back left
	{-1, 1, 1}  --v8 - top back left
}

Then all you have to do is multiply each vertex by the corresponding value:

for _, Vector in pairs(Vertices) do
    local CornerPos = (Part.CFrame * CFrame.new(Size .X/2 * Vector[1], Size .Y/2 * Vector[2], Size .Z/2 * Vector[3])).Position
end

That will get you all the corners, no matter the rotation.


How this script works is that you will fetch one corner and multiply it by a value. For example, if I wanted to get the bottom front right corner, then I would multiply the top front right corner(the corner got from doing local CornerPos = (Part.CFrame * CFrame.new(Size .X/2, Size .Y/2 , Size .Z/2)).Position, by that. So if the top front right corner had a position of 10, 10, 10, then the bottom front right corener would have a position of 10, -10, -10.

27 Likes

Hi, this works in a fashion. I’m faced with this issue:

Inserted the attachments solely as reference.

local Part = script.Parent

local Vertices = {
	{1, 1, -1},  --v1 - top front right
	{1, -1, -1}, --v2 - bottom front right
	{-1, -1, -1},--v3 - bottom front left
	{-1, 1, -1}, --v4 - top front left
	
	{1, 1, 1},  --v5 - top back right
	{1, -1, 1}, --v6 - bottom back right
	{-1, -1, 1},--v7 - bottom back left
	{-1, 1, 1}  --v8 - top back left
}

for _, Vector in pairs(Vertices) do
    local CornerPos = (Part.CFrame * CFrame.new(Part.Size.X/2 * Vector[1], Part.Size.Y/2 * Vector[2], Part.Size.Z/2 * Vector[3])).Position
	
	local AT = Instance.new("Attachment")
	AT.Visible = true
	AT.WorldPosition = CornerPos
	AT.Parent = Part
end