Pet Following System Not Rotating Pets Correctly?

What its doing:

image

Script:

function PetClass:FollowCharacter()
	if self.Connection ~= nil then
		self.Connection:Disconnect()
	end

	self.Connection = RunService.Heartbeat:Connect(function(deltaTime: number)
		local folder = self.RenderedPets
		local character = self.Player.Character

		-- Define the number of rows and columns for the pets
		local numRows = 2  -- Adjust as needed
		local numColumns = 2  -- Adjust as needed
		local distance = 3  -- Adjust the distance between pets as needed

		local function getPosition(row, col, distance)
			-- Calculate the position for each pet in columns centered behind the player
			local xOffset = (col - (numColumns + 1) / 2) * distance
			local zOffset = (row - 0) * distance
			return xOffset, zOffset
		end

		local playerPosition = character.PrimaryPart.Position

		for i, pet in pairs(folder:GetChildren()) do
			-- Calculate the row and column for the pet
			local row = math.floor((i - 1) / numColumns) + 1
			local col = (i - 1) % numColumns + 1

			local _, characterSize = character:GetBoundingBox()
			local _, petSize = pet:GetBoundingBox()

			-- Calculate the position for the pet in columns centered behind the player
			local xOffset, zOffset = getPosition(row, col, distance)

			-- Adjust the yOffset as needed (pets should be at the same vertical level as the player)
			local yOffset = 0
			-- Calculate the pet's position directly behind the player
			local petPosition = playerPosition - character.PrimaryPart.CFrame.rightVector * xOffset - character.PrimaryPart.CFrame.lookVector * zOffset
			local playerOrientation = character.PrimaryPart.CFrame.lookVector
			pet:SetPrimaryPartCFrame(CFrame.new(petPosition.x, playerPosition.y + yOffset, petPosition.z) * CFrame.Angles(0, (math.atan2(playerOrientation.X, playerOrientation.Z) - 200), 0))

		end
	end)
end

Does not print any errors.

Hi Blade,
You need to elaborate on your expectation

From the title (and the photo from their game attached), I can tell that they need help with pets being orientated the correct way (facing the same direction as the player).

The issue with the pets not rotating correctly around the player might be due to the way the orientation of the pet is being set. The line of code that sets the pet’s orientation is:

pet:SetPrimaryPartCFrame(CFrame.new(petPosition.x, playerPosition.y + yOffset, petPosition.z) * CFrame.Angles(0, (math.atan2(playerOrientation.X, playerOrientation.Z) - 200), 0))

The CFrame.Angles function is used to create a rotation CFrame, but the arguments passed to it seem to be incorrect. The math.atan2(playerOrientation.X, playerOrientation.Z) - 200 part is likely causing the issue.

The math.atan2 function returns the angle in radians between the positive x-axis and the point given by the coordinates (playerOrientation.X, playerOrientation.Z). Subtracting 200 from this value doesn’t make sense because the result of math.atan2 is in radians and 200 is likely intended to be in degrees.

To fix this issue, you should convert the 200 degrees to radians before subtracting it from the result of math.atan2:

local degreesToRadians = math.pi / 180
pet:SetPrimaryPartCFrame(CFrame.new(petPosition.x, playerPosition.y + yOffset, petPosition.z) * CFrame.Angles(0, (math.atan2(playerOrientation.X, playerOrientation.Z) - 200 * degreesToRadians), 0))

This should fix the issue with the pets not rotating correctly around the player.

1 Like

Try this …

Script
function PetClass:FollowCharacter()
	if self.Connection ~= nil then
		self.Connection:Disconnect()
	end

	self.Connection = RunService.Heartbeat:Connect(function(deltaTime: number)
		local folder = self.RenderedPets
		local character = self.Player.Character

		-- Define the number of rows and columns for the pets
		local numRows = 2  -- Adjust as needed
		local numColumns = 2  -- Adjust as needed
		local distance = 3  -- Adjust the distance between pets as needed

		local function getPosition(row, col, distance)
			-- Calculate the position for each pet in columns centered behind the player
			local xOffset = (col - (numColumns + 1) / 2) * distance
			local zOffset = (row - 0) * distance
			return xOffset, zOffset
		end

		local playerPosition = character.PrimaryPart.Position

		for i, pet in pairs(folder:GetChildren()) do
			-- Calculate the row and column for the pet
			local row = math.floor((i - 1) / numColumns) + 1
			local col = (i - 1) % numColumns + 1

			local _, characterSize = character:GetBoundingBox()
			local _, petSize = pet:GetBoundingBox()

			-- Calculate the position for the pet in columns centered behind the player
			local xOffset, zOffset = getPosition(row, col, distance)

			-- Adjust the yOffset as needed (pets should be at the same vertical level as the player)
			local yOffset = 0

			-- Calculate the pet's position directly behind the player
			local petPosition = playerPosition - character.PrimaryPart.CFrame.rightVector * xOffset - character.PrimaryPart.CFrame.lookVector * zOffset

			-- Calculate the rotation for the pet to face the same direction as the player
			local playerOrientation = character.PrimaryPart.CFrame.lookVector
			local petRotation = CFrame.new(petPosition, petPosition + playerOrientation)
			pet:SetPrimaryPartCFrame(CFrame.new(petPosition.x, playerPosition.y + yOffset, petPosition.z) * petRotation)
		end
	end)
end

Also heartbeat seems excessive …