Hello. I am building a track runner game (Like temple run). I am having some problems with the generation script. I don’t know the logics behind this type of generation. Everything generates on the wrong place. This is my code:
local Track = game.ServerStorage.Track
local Position = CFrame.new(0,0,0)
for i = 0, 100 do
local items = Track:GetChildren()
local TrackPart = items[math.random(1, #items)]:Clone()
TrackPart.Parent = game.Workspace
TrackPart:SetPrimaryPartCFrame(Position)
if TrackPart.Name == "Left" then
Position = Position + Vector3.new(25, 0, 0)
Position = Position*CFrame.Angles(0, math.rad(-90), 0)
end
if TrackPart.Name == "Right" then
Position = Position + Vector3.new(-25, 0, 0)
Position = Position*CFrame.Angles(0, math.rad(-90), 0)
end
if TrackPart.Name == "Straight" then
Position = Position + Vector3.new(25, 0, 0)
end
print(Position)
end
you may have to do a seperate calc using cframe.angles or something like cframe.new(0,0,0) * cframe.angles. which would change the angle section of the cframe and leave the position part
One thing wrong with your code is that the right and left code both rotate by -90 degrees. You should make the right one 90 instead of -90. You also should use lookVector and right vector to align with the updated frame instead of 3d world space. Once the track rotates to the left, your “Straight” is actually right. With these changes, your code looks like this:
local Track = game.ServerStorage.Track
local Position = CFrame.new(0,0,0)
for i = 0, 100 do
local items = Track:GetChildren()
local TrackPart = items[math.random(1, #items)]:Clone()
TrackPart.Parent = game.Workspace
TrackPart:SetPrimaryPartCFrame(Position)
if TrackPart.Name == "Left" then
Position = Position - (Positon.rightVector * 25)
Position = Position*CFrame.Angles(0, math.rad(-90), 0)
end
if TrackPart.Name == "Right" then
Position = Position + (Position.rightVector * 25)
Position = Position*CFrame.Angles(0, math.rad(90), 0)
end
if TrackPart.Name == "Straight" then
Position = Position + (Positon.lookVector * 25)
end
print(Position)
end
This works in my demo, however I have yet to see what your object hierarchy looks like (the workspace in the explorer tab).
I breezed through the explanation a bit. If you have more specific question about how this all works, I’m happy to answer them.