How to reliably obtain the Y vector of any model every time no matter the orientation

I have a small issue where on my towers (See below), the enemy is meant to path find to that red dot. With the ‘crossbow tower’ this is done correctly.

However for most other builds the pathfind is wayyy down below for some reason, despite using the same math and having from what ive seen, the exact same orientation when placed, being 0,-90,0 for all builds, both on its primarypart and the model.

image

image

image

image

image

in the last image on the side, it accurately does the Y vector based on its orientation, but its still WAY UNDER the actual build

        local targetcframe,modelsize = model:GetBoundingBox()
	local yaxis = targetcframe.YVector * (modelsize.Y / targetmultiplier)
	local position = (target.Position - yaxis)

The target multiplier is 2.2 (2 would mean the absolute bottom position internally of the models height, I want it slightly above so 2.2)

Its at 2.2 for every single build, even the ones where its offset massively but i do not understand why

for debugging (the red dot) this is the code i run

        local primary_part = model
	local new_part = Instance.new("Part")
	local highlight = Instance.new("Highlight")
	highlight.Parent = new_part
	highlight.Adornee = new_part
	highlight.FillTransparency = 1
	highlight.OutlineTransparency = 0
	highlight.OutlineColor = Color3.new(1, 0, 0)
	new_part.Size = Vector3.new(0.5,0.5,0.5)
	new_part.Position = position
	new_part.Anchored = true
	new_part.BrickColor = BrickColor.new("Bright red")
	new_part.Parent = primary_part

I would like if anyone knows a more reliable way to do this exact same thing, to share code which potentially works, or to adapt what I have, and if possible to make it use fewer resources as I run this on the server quite often for each npc.

1 Like

Does your model have a PrimaryPart selected? If it doesn’t the script may be selecting the model by a random Part inside it which could affect the model’s Position and the pathfinding to get there.

And try selecting each of the tower Models with the Move tool.
See where the tool arrows are aligned to. You may have inadvertently edited the Pivot of the model.

i think its because the bounding box doesnt rotate with the model, so the Y size of the bounding box is no longer the height of the building when rotated sideways

Try using modelsize.X or modelsize.Z based on which direction its facing

All of them have their primary parts defined and the move tool shows its in the correct position

image

image

image

so it cant be a technical issue unless you have other ideas

I had tried changing both the Vectors with lookvector, Xvector Zvector ect, and even modelsize z,y,x ect but it ends up being the same issue but on a different axis, i ahve no idea why for some builds its correctly inside it, and for others its outside

Also rotation doesnt seem to be an issue it can be rotated 40 degrees or 111 degrees, the dot is aligned on the models Xy or z correctly

image

Try using GetPivot instead to get the cframe you want, this function can also be used on both models & parts. Then you can use GetExtentsSize for getting the size of the model, and just the Size property for parts.

Also for the position variable, you should try (target.CFrame * CFrame.new(0, -(modelsize.Y / targetmultiplier), 0)).Position instead of (target.Position - yaxis). (And maybe split that into two or three variables like offset, cframe, and position)