Hey Devforum,
How do I find the nearest part to an object?
Thanks,
Sasial.
function nearest(p)
local n = math.huge
local np = p
for i, v in pairs(workspace:GetDescendants()) do
if v:IsA('BasePart') and v ~= p and (v.Position - p.Position).magnitude < n then
n = (v.Position - p.Position).magnitude
np = v
end
end
return np, n
end
How would I make it so that it’s the nearest part in a folder called “Nodes” in the workspace?
I normally can script well…
Being late a night makes it hard to script.
I’ve never had experience with this sorta stuff.
If someone can explain this, it would help alot.
Replace workspace:GetDescendants()
with the folders children you want to go through. So examplefolder:getchildren(). When you call the function you are going to give the part that you wsnt to find the nearest part to, it then goes through the workspace, gets the magnitude of every part and sets “n” to equal the lowest magnitude. Once it’s done going through every part it returns the part and the magnitude. Check out more on Magnitude here.
Thanks so much!
Greatly appreciated.
How would you make it so that the part is not behind. (It can be on the side.)
I’m not quite sure I understand your question here? As in the part is rotated on its side? If so the orientation of the part doesn’t matter it should still work.
If you create a position vector s = (x, y, z) and dot it with the look vector then you can check if the object is behind you or not. It is simple linear algebra. For example,
local s = part.Position - character.Position -- position vector from player to the part
if s:Dot(character.CFrame.LookVector) > 0 then -- if the dot product > 0, then the object is **in front**.
-- code in here
end
Hope this makes sense and is helpful
Thanks, how would I allow it to also be on the sides; just not behind?
Just worth mentioning, “nearest part to an object” can be interpreted different ways. The solutions given so far are assuming (part.Position - object.Position).Magnitude
, which is the distance between the bounding box centers. This is very different from, say, the part whose bounding box center is nearest to the surface of a large object, or the parts whose surfaces are nearest to touching (with or without overlapping). This is why real use case examples can be helpful to include in your questions, so that the problem is not ambiguous or overly generalized. Some of the less-trivial intepretations of the question might require raycasts or more maths than just a vector magnitude.
Yes this is an important point. Point-to-surface distances cannot use a simple distance formula. A little clarity will be helpful to know whether the surfaces of the objects in question are complex or not, and if surfaces need to be considered in the first place.
Seeing that you are asking this, as I already gave you an answer, it seems as though you are not aware of what dot product does or is capable of. There are countless posts explaining this, and a quick google search can help you understand a simple math equation, especially given the formula and function that I provided.
It’s good to ask, but I would suggest a little more effort on your part for basic needs such as universal math knowledge (sorry if I didn’t imply this as clearly as I thought by saying “basic linear algebra”).
To save you 2 seconds of your time google searching, the dot product between two vectors (the position vector and the look vector) returns a value determining how close together they are, and whether they point in the same direction. When the dot product returns a positive value, then the part is in front of the player. By “in front”, I mean 180 degrees from left to right. This includes things “on the sides” that are still ahead of the player.
I’m glad you ask questions, some people are shy to ask and that is a terrible mentality. But I suggest you put a little more effort into figuring things out on your own first before striking the forums, as it will save you time (google) and it will make you a better scripter overall.
Hope this helps
Thanks so much.
Didn’t understand the maths behind it. (: