Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to have pet/model not rotate with character?

Asked by 5 years ago

I am current creating a pet system where you equip pets and the pets will follow you in game. I have that all finished, but I came across another problem while finishing up. The problem is when you rotate your character in game, the pets will rotate with the character since its CFrame is relative to the HumanoidRootPart. I am using BodyPosition and BodyGyro to position the pets.

Here is an example of the current situation: https://gyazo.com/3cb1673192f5a48bf46fbfacb82011e0

What it is supposed to look like: (another game) https://gyazo.com/a026a711b28fa4c5bd5849cd43495e07

notice that the pets are supposed to be stationary when you rotate your character

The Server Code

local Pets = script.Parent:WaitForChild("Pets") -- pets folder


while wait() do
    for i,pet in pairs(Pets:GetChildren()) do
        if pet:IsA("Model") then
            local HumRP = script.Parent:WaitForChild("HumanoidRootPart")
            local petz = pet.PrimaryPart
            local BodyPosition = petz.BodyPosition
            local BodyGyro = petz.BodyGyro

            BodyPosition.Position = HumRP.CFrame * Vector3.new(2 + (i*3), 1, 2) 
            BodyGyro.CFrame = CFrame.new(pet.PrimaryPart.Position, HumRP.Position)

            local amount = #Pets:GetChildren()
            local offset = 5

            for angle = 1, 360, (360/amount) do -- have pets in circle formation and set positions
                BodyPosition.Position = (HumRP.CFrame
                * CFrame.Angles(0, math.rad(-90) + ((360 / amount) * math.rad(i)), 0)
                * CFrame.new(0, 2, 0 / amount + offset)).p
            end
        end
    end
end

If anyone knows how I can fix this code please tell me. I need it so that when your character rotates, the pets do not rotate with it. I am not very good in dealing with CFrames and such. I have been stuck on this for a while now and am not sure how to fix it, so any help would be appreciated. Thanks.

If you have any questions feel free to ask me.

0
try commenting out/ removing the cframe.angles part because i'm sure that controls rotation AnonymousDeveloper13 22 — 5y

Answer this question