Position Characters In Straight Line

I would like my player characters to be able to line up behind one another, based on their index in my queue table.

My current code is:

local frontOfQueuePart = workspace.Part1
local spacing = 2
local queue = {}

for i,player in ipairs(queue) do
    local pos = frontOfQueuePart.Position + Vector3.new(0, 0, 1) * spacing * (i-1)
    player.Character.Humanoid:MoveTo(pos, frontOfQueuePart)
    player.Character.Humanoid.MoveToFinished:Wait()
    player.Character:PivotTo(CFrame.new(pos))
end

The problem with this code is that my front of queue part has an Orientation of (0, -45, 0). This means that adding to only the Z axis causes this diagonal line as seen below.

How can I make my characters line up in a straight line (directly behind one another) relative to the original CFrame of the part at the front of the queue?

Thanks in advance :slightly_smiling_face:

1 Like

You need to use the actual direction you want the queue to extend in, like taking the -LookVector of some part that represents both the position and orientation of the first avatar in the line. Using Vector3.new(0,0,1) will make the queue always extend in the +Z world-space direction, not relative to something in the scene.

Try:

local pos = frontOfQueuePart.Position - frontOfQueuePart.CFrame.LookVector * spacing * (i-1)

And notice that rotating the frontOfQueuePart lets you rotate the queue however you want.

1 Like

You can use the CFrame’s LookVector to create an offset relative to where it’s pointing.

For example, to place a objectA 1 stud away from the direction objectB is looking, regardless of its orientation, you can take the LookVector of objectB and add it to its position.

local objectA = SomePart
local objectB = SomePart

local distance = 1

-- Sets objectA `distance` studs away from objectB relative to where its looking
objectA.Position = objectB.Position + objectB.CFrame.LookVector * distance

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.