How to measure magnitude only by X?

Hey. It’s February already can you believe it? Anyways.

My question or problem if you like, so I am making a battle royale game. And the barrier in my game functions like this:

Find the middle of the whole map:

local model = workspace.Model 
local orientation = model:GetBoundingBox() 
print(orientation)

Like this, but when you have some tall mountain around the map the Y value really gets a lot higher.

So… what’s the problem you ask?

And I answer:
On my map, there are different terrain levels. So you can stand on a tall building, or you can be deep down inside shrek’s nasty swamp.

Which leads me to this scientific and informative infographic:

Allow me to explain, the orange star and triangle represent 2 different players.
Black line in the middle, is the middle of the map where the barrier aims towards. And the red line is the barrier. The red circle in the middle is B A S I G L Y center/middle of the model/map. And I repeat: the middle of the map “Part”'s Y value is much higher since there are mountains around.

My script functions like this:

    • Check the magnitude between center of map part and the player root part. (get the distance in studs)
    • Get the barriers X size divided by 2 (radius of the barrier/ second grade math)
    • If the distance/studs number is between center of map and player is bigger than the barrier radius.
      It means that they are outside of the barrier.

But that’s were the fun ends.
If you listened / read closely, I wrote this:

If the distance/studs number is between center of map and player is bigger than the barrier radius.
It means that they are outside of the barrier.

But that is the entire truth. Now, if we jump back to the picture again:

You can see that the “star” player is mathematically further away. But technically he is still in the zone, but the programming doesnt care about that. It basically measures that the distance of the “star” player is from the center is bigger than the radius of the barrier. Thinking that the “star” player is outside the barrier, it removes health from the player. Look at the green dotted lines, if you would straighten up that line it would be outside of the barrier. (just for informational purposes). And the triangle player which is higher up, hence, closer to the center of map is “Mathematically” closer, but the distance from player to barrier is the same as the star player.

I hope you get what I mean. So is there any way to measure the distance on an X or Y axis instead of taking the height into account, because both players are really the same lengts away from the barrier.

I really hope you get what I mean. All help is appreciated!

My current script:
while true do

        for i, player in ipairs(game.Workspace.PlayersInGame:GetChildren()) do
         
            if player then
	
                local hrp = player:WaitForChild("HumanoidRootPart")
                local positionFromMiddle = (hrp.Position-centerOffMap.Position).magnitude
				
 				if positionFromMiddle > BARRIER.Size.X/2 and #playersInGame:GetChildren() > 1 then
				print(positionFromMiddle .. " position from middle")
				print(BARRIER.Size.X/2 .. " barrier size")
                    player:WaitForChild("Humanoid"):TakeDamage(10)

                end

            end

        end
2 Likes

You could do it the same way as you currently are, but setting the height axis to 0. Or even better, set it to be the same as one of the components you are comparing against, this way you reduce the number of vector3s you need to construct

playerPosition = Vector3.new(playerPosition.X, center.Y, playerPosition.Z)
distanceFromCenter = (center - playerPosition).magnitude

This way you are working on a XZ plane instead of XYZ space

Edit: I have reread your post and maybe my answer wasnt really what you were asking for, if you want to measure distance only in a single axis you can just substract the 2 single axis (i.e x1 = 1, x2 = 3) points and use the result as the distance.

3 Likes

My current script:
while true do

        for i, player in ipairs(game.Workspace.PlayersInGame:GetChildren()) do
         
            if player then
	
                local hrp = player:WaitForChild("HumanoidRootPart")
                local positionFromMiddle = (hrp.Position-centerOffMap.Position).magnitude
				
 				if positionFromMiddle > BARRIER.Size.X/2 and #playersInGame:GetChildren() > 1 then
				print(positionFromMiddle .. " position from middle")
				print(BARRIER.Size.X/2 .. " barrier size")
                    player:WaitForChild("Humanoid"):TakeDamage(10)

                end

            end

        end

How would I implement that in my script? Thank you for the answer by the way!

Just to confirm, your problem is that your current approach takes into account the varying heights of the players into the math and as a result it thinks players are further away than they are?
Also is your barrier circular or is it kind of like a wall?

1 Like

Its circular, and yes,

Correct,

What @Roytt is trying to say is you need to use the projection of the player’s position on the XZ plane. You can cancel out vector components by multiplying them by identity vectors.

for i, player in ipairs(game.Workspace.PlayersInGame:GetChildren()) do
	local hrp = player:WaitForChild("HumanoidRootPart")
	local positionFromMiddle = ((hrp.Position - centerOffMap.Position) * Vector3.new(1, 0, 1)).magnitude
	if positionFromMiddle > BARRIER.Size.X/2 and #playersInGameGetChildren() > 1 then
		print(positionFromMiddle .. " position from middle")
		print(BARRIER.Size.X/2 .. " barrier size")
		player:WaitForChild("Humanoid"):TakeDamage(10)
	end
end
1 Like