I’m developing an American football game and I’m wondering the best method to figure out a good way to calculate where you’re at on the field based on your position.
I’m not asking for a formula or anything, just any suggestions on how to go about it with the most accurate result.
(Sorry if I’m being unclear about what I mean by yards if you’re unfamiliar with American football)
Edit for clarification: I’m looking for someone to help point me in the right direction in how I determine the player’s position relative to the field’s position. I’ve been playing around with :ToObjectSpace and :ToWorldSpace but I’m bad with math. The field size is exactly 285 studs long, and each yard is 3 studs.
I’m more so looking for a way to tell what yard line the player is at. The way my field is set up is where every 3 studs turns out to be 1 yard.
What I’m wanting to know how I determine what yard line the player is at on a field with 285 studs based off of solely their HumanoidRootPart position. What’s especially confusing is how can I accomplish this when there are sides of the field each with a ten yard line, twenty yard line, thirty yard line, and forty yard line.
Essentially, how can I use the player’s torso position to find out if they’re standing at the five yard line or the thirty yard line and which side of the field they’re on.
.Magnitude should be useful for you here;
You can compare things distances using it, and compare people’s locations and stuff, to points on the map;
Edit; You would need more than just that, because it counts studs and if you’re for instance a little bit more higher up it could count as more studs giving an inaccurate reading; You would need probably to set these comparators Y locations to whatever what that player’s Y location before using .Magnitude;
So like,
local newVector3 = Vector3.new(comparator.Position.X, comparator.Position.Y, HumanoidRootPart.Position.Z)
comparator.Position = newVector3
local area = (math.floor(3 / (comparator.Position + HumanoidRootPart.Position).Magnitude)) * 10
area = area - 50 -- Offset it
I’ve been tampering with your code and suggestion and I haven’t gotten anything to work using .Magnitude. Your psuedo code, while I understand it’s psuedo code and it isn’t meant to work entirely, hasn’t been able to work for me even when modifying it. It just prints 0 no matter where the player is.
Test = function(character)
local HumanoidRootPart = character.HumanoidRootPart
local comparator = game.Workspace.Field.Grass.Normal.Mine
local newVector3 = Vector3.new(comparator.Position.X, comparator.Position.Y, HumanoidRootPart.Position.Z)
comparator.Position = newVector3
local area = (math.floor(3 / (comparator.Position + HumanoidRootPart.Position).Magnitude)) * 10
area = area - 50 -- Offset it
print(area)
end
spawn(function()
while true do
wait(1)
for index, value in pairs(game.Players:GetPlayers()) do
if value and value.Character then
Test(value.Character)
end
end
end
end)
I removed my edits and just implemented your psuedo code which is printing -50 constantly no matter the movement of the character.
What you can do is constantly cast a ray from the player’s root part downwards with all the yard lines as the whitelist, this should give you what yard line the player is at.
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Whitelist
rayParams.FilterDescendantsInstances = workspace.YardLinesFolder
while true do
local result = workspace:Raycast(humanoidRootPart.Position, -humanoidRootPart.CFrame.UpVector * 40, rayParams)
if (result) then
print("Player is at " .. result.Instance.Name)
end
wait(.2)
end