I added huamnoid and humanoid rootpart to the non-human dragon and used moveTo to follow the player. Why does the pet always turn its head when the player moves? As in the video above, is there any good way to solve this problem?
I added huamnoid and humanoid rootpart to the non-human dragon and used moveTo to follow the player. Why does the pet always turn its head when the player moves? As in the video above, is there any good way to solve this problem?
When creating a scripting feedback post, send the code so others can review it and identify the problem.
I suggest moving the Pivot to the player instead of using :MoveTo() as it is poorly designed. MoveTo also has a limit of 8 seconds so if it exceeds the limit it will stop following the player. The reason it turns around is because I am assuming you are using MoveTo everytime a player starts moving?
local function moveParts()
for i, part in ipairs(workspace.Folder:GetChildren()) do
task.spawn(function()
local PetCount = #workspace.Folder:GetChildren()
while task.wait() do
local playerPosition = Character.HumanoidRootPart.Position
local playerForward = Character.HumanoidRootPart.CFrame.LookVector
local playerRight = Character.HumanoidRootPart.CFrame.RightVector
local followPositions = {}
local maxColumns = 3
local maxRows = math.ceil(5 / maxColumns)
local offsetX = (maxColumns - 1) * 5 / 2
local row = math.ceil(i / maxColumns)
local column = (i - 1) % maxColumns
local rowPetCount = math.min(PetCount - (row - 1) * maxColumns, maxColumns)
local xOffset = (column - (rowPetCount - 1) / 2) * 10
local zOffset = (1-row) * 10
local followPosition = playerPosition - playerForward * 20 + playerRight * xOffset + playerForward * zOffset
followPositions[i] = followPosition
if player.Character.Humanoid.MoveDirection.Magnitude == 0 then
continue
else
part.Humanoid:MoveTo(followPosition)
end
end
end)
end
end
moveParts()
for i,v in pairs(workspace.Folder:GetChildren()) do
v.Humanoid.MoveToFinished:Connect(function(bool)
if bool == true and Character.Humanoid.MoveDirection.Magnitude == 0 then
v:SetPrimaryPartCFrame( CFrame.lookAt(v.PrimaryPart.Position,Character.PrimaryPart.Position))
return
end
end)
end my script
Yes, can Pivot achieve the effect of MoveTo
Yes, this is possible
You can use (playerPos - enemyPos).Unit
to detect the direction the player is in, after that simply do:
local newPos = enemyPos + lookDirection * speed -- configure this depending on your needs
enemy:PivotTo(CFrame.new(newPos))
You can loop this.
PivotTo has a lot of pros compared to MoveTo, but there are some cons too. If I remember right, MoveTo should have better pathfinding, so it might navigate better in certain situations.
That said, I’d probably go with PivotTo. Just make sure to teleport the pet back to the player if it falls too far behind or struggles to keep up.
If this works for you, I can come up with something to help. Credit for PivotTo definitely goes to Alonely since it was their idea, but I can add the bonus feature to make sure the pet keeps up with you.
So, if I do this give @ALonelyGalaxy the solution please.
You can try this
local RunService = game:GetService("RunService")
local function moveParts()
local pets = workspace.Folder:GetChildren()
local PetCount = #pets
if PetCount == 0 then return end
local maxColumns = 3
local playerRootPart = Character.HumanoidRootPart
RunService.Heartbeat:Connect(function()
local playerPosition = playerRootPart.Position
local playerForward = playerRootPart.CFrame.LookVector
local playerRight = playerRootPart.CFrame.RightVector
for i, part in ipairs(pets) do
local row = math.ceil(i / maxColumns)
local column = (i - 1) % maxColumns
local rowPetCount = math.min(PetCount - (row - 1) * maxColumns, maxColumns)
local xOffset = (column - (rowPetCount - 1) / 2) * 10
local zOffset = (1 - row) * 10
local followPosition = playerPosition - playerForward * 20 + playerRight * xOffset + playerForward * zOffset
local petPosition = part.PrimaryPart.Position
local distanceToPlayer = (petPosition - playerPosition).Magnitude
if distanceToPlayer > 50 then
local teleportPosition = playerPosition - playerForward * 10
part:PivotTo(CFrame.new(teleportPosition))
else
local currentCFrame = part.PrimaryPart.CFrame
local targetCFrame = CFrame.new(followPosition, playerPosition)
part:PivotTo(currentCFrame:Lerp(targetCFrame, 0.1))
end
end
end)
end
moveParts()
Runservice…Heartbeat should be smoother and more consisten than Task.wait
Also Instead of spawning a seperate thread for each pet task.spawn
the RunService should manage the loop for all pets. Im quite positive this will significantly reduce the CPU usage.
I like to leave room for error tho and say i could be wrong…
How can we achieve it? Thank you
Look at @HolyGenki reply right above you. You can also check mine for the basic understanding.
Thank you, I will try to see what kind of effect it will have
This effect is similar to AlignPosition, but it does not have the process of moving like MoveTo
Yeah, its because the Lerp
, I usually see it used when we are using the PivotTo instead of the MoveTo it helps Smooth Transitions and gives more Fine Control and is light weight compared to a Tween.
I got mixed feelings on how i think we should proceed.
Have you played this game before? His pet following doesn’t feel like it’s done with Tween or LERP, it feels like it’s done with MoveTo
I highly doubt it was made by MoveTo since it is a very poor function but if you want to use MoveTo you can listen when it ends (8 second timeout) and reactivate it.
We can make that happen, but you will need to change your model up some. XD
i will give up more details in a bit, I should’ve seen this before i did anything else. I dont think we can Get this solution with just simply MoveTo or PivotTo without the adjustments to your model first.
I’m actually quite confident it can be done with move to though there is a KEY trick to it that i don’t see many users knowing, its more of a model issue.
This is not the same as what we are about to do, XD i personally feel like this is a slightly different request but i got you
I already got the key script made, I want to add some bonus handling features, to help prevent potential errors.
I personally Think the right thing to do though is mark this post as solved give credit to @ALonelyGalaxy
Make a new post and DM me saying you want it like that pet.
Only because what your originally requested is not the same as what we’re about to all do its simple and I think you will understand more once I give you the core details.
You defined helped him more than I did, you should get marked.
It was your idea that solved what he originally asked. I just help to help people. Mark doesn’t really matter to me like that any ways.
Either way in technical terms your original answer is what would fix his issue he made the post for and if someone views this post it should be the answer they see. What I would be giving him for this post should be for different post for sure.