Need help with NPC aligning

image
Im trying to make the pets align like the red half circle drawn in the reference image. and also make them face the player’s direction as soon as they reached the moveToPosition.
Here is the code:

local function getPosition(angle, radius)
	local x = math.cos(angle) * radius
	local z = math.sin(angle) * radius
	return x, z
end

local function positionPets(character, folder)
	local circle = math.pi
	local time = tick()

	local forwardVector = character.PrimaryPart.CFrame.LookVector

	for i, pet in pairs(folder:GetChildren()) do
		local radius = 4 + #folder:GetChildren()
		local angle = i * (circle / #folder:GetChildren())
		local x, z = getPosition(angle, radius)
		local _, characterSize = character:GetBoundingBox()
		local _, petSize = pet:GetBoundingBox()

		local offsetY = -characterSize.Y / 2 + petSize.Y / 2
		local sin = (math.sin(15 * time + 1.6) / 0.5) + 1
		local cos = math.cos(7 * time + 1) / 4

		for _, v in ipairs(pet:GetDescendants()) do
			if v:IsA("BasePart") then
				v.CollisionGroup = "Mobs"
				if v.Name == "HumanoidRootPart" then
					v.Anchored = false
				end
			end
		end

		local moveToPosition

		if character.Humanoid.MoveDirection.Magnitude > 0 then
			moveToPosition = character.PrimaryPart.CFrame:PointToWorldSpace(Vector3.new(x, offsetY / 2 + math.sin(time * 3) + 1, z))
		else
			moveToPosition = character.PrimaryPart.CFrame:PointToWorldSpace(Vector3.new(x, offsetY / 2 + math.sin(time * 3) + 1, z))
		end
		pet.Humanoid:MoveTo(moveToPosition)
	end
end```
1 Like

Hello!

I think the concept below solves your issue.

local Parts = 5;
local MainPart = script.Parent;

local Radius = Parts + 5; -- Change + 5 to your liking.

for i = 1, Parts do
	local Part = Instance.new("Part", game.Workspace);
	
	Part.Anchored = true;
	Part.Size = Vector3.new(1, 1, 1);
	
	local Angle = math.rad((i - 1) * (180 / (Parts - 1))); -- Calculate the angle (180, half a circle). Important is that you substract -1 from parts, not the radius.
	local X = Radius * math.cos(Angle);
	local Y = Radius * math.sin(Angle);
	
	Part.Position = MainPart.Position + Vector3.new(X, 0, Y);
	Part.CFrame = CFrame.lookAt(Part.Position, MainPart.Position);
end

Let me know if anything is unclear, and let me know if it worked!

1 Like