how would you use magnitude to tell if you are about 3 studs away from a part and then if you are something will happen.
magnitude seems interesting
I thing its how form apart things between each other are though...……….
how would you use magnitude to tell if you are about 3 studs away from a part and then if you are something will happen.
magnitude seems interesting
I thing its how form apart things between each other are though...……….
This is probably the easiest way to do it.
local magnitude = (part1.Position - part2.Position).magnitude
if magnitude<=3 then
--whatever it is you want to do
end
You can read more about it here: https://developer.roblox.com/articles/Magnitude
Pretty easily it looks like
local part = workspace.Part --The part you're checking distance to
local radius = 3 --The radius or distance from the part
local characterRootPart --You'll have to set this or find it on your own.
function checkPosition() --Start of a function
--[[
This works by subtracting the part vectors and then using the distance formula
it will get the length of the resulting vector and compare it to your radius.
(.magnitude gets the length of any vector)
(part.Position-characterRootPart.Position) makes a vector which could be seen as the change needed to get from vector A to vector B.
Which means that taking the size of the resulting vector is our distance.
]]
return (part.Position-characterRootPart.Position).magnitude<=radius
end--End of a function
if checkPosition() then --call the function and check whether or not it's true
--do something
else
--don't do something
end
Here's a good visual on vector subtraction:
ok so can you explain what lines 1 -7 mean plz because I want to get a better understanding of magnitude please
oh and also could this be a server script or lacal script too?
I'll add a couple more comments to my code
This can be either, it depends on what you use it for afterwards.
oh ok thank you Vathriel for helping! Is magnitude a really hard topic or is it medium or lightweight in your oppinioin?
It's not too complex relative to other programming topics, this is more complex on the linear algebra side of things than programming.
One thing I forgot to mention is that it doesn't actually matter which order you do the subtraction in because if you look at the distance formula the squares remove any negatives.
oh ok.So Basicaly it is the distance formular between two points? just subtract one position from another's magnitude to get the lenght in distance between the two points.Is that it? is that what im spupposed to be getting?
Yeah that's about right. That's why the subtraction is necessary, because you can use part.Position.magnitude to get the distance from the origin of the game.
oh so instead of "part.Position-characterRootPart.Position).magnitude" why couldnt you just put "part.Position.magnitude -characterRootPart.Position).magnitude"? what would happen if you put magnitude at the end of part?
Well .magnitude returns a single number which is the length of the vector. So you could have 2 vectors A and B be 4 and 5 studs away from the origin respectively and if you did A.magnitude-B.magnitude you'd get 1, which might not be right since one could be 4 studs away in the negative direction and one could be 5 studs away in the positive direction meaning they might instead be 9 apart.
Which as a side effect gets even more complicated in 3D like roblox. That's why we subtract first instead of afterwards.
I updated my post with a visual of vector subtraction
ohohoh ok :D
Glad I could help!
This discussion is preserved as it was. New answers, comments, and votes are closed.
1 comment
If there is nothing more you need, then can you accept my answer? This has been unanswered for 8 days.