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

Rotating pet where your head is pointing?

Asked by
Sougood -10
5 years ago

I am trying to make pet( the pet is model with primarypart) but, I need to make it rotate where you look. I don't know how and I am asking for help.

Code :

local Pet = script.Parent
local Move = script.Parent:WaitForChild('Move')

Move.OnServerEvent:connect(function(Player)
    if workspace[Player.Name] then
        local Head = workspace[Player.Name]:FindFirstChild('Head')
        if Head then
            Pet:SetPrimaryPartCFrame(CFrame.new(Head.CFrame.X + 1,Head.CFrame.Y +                                           2,Head.CFrame.Z + 1))
            Pet:SetPrimaryPartCFrame(Pet.Hitbox.CFrame * CFrame.Angles(0,Head.CFrame.lookVector.Z,0))
        end
    end
end)

Please help!

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Hi Sou,

You can make the head rotate easily by using the simple lookAt method. The lookAt method is basically using a CFrame with 2 values, one is the position it's staying and the other is the position it's looking at. So, it would be something like this:

local part_to_lookat = head.Position;
local pos_to_stay = primpart.CFrame.p;

primpart.CFrame = CFrame.new(pos_to_stay, pos_to_lookat);

So in your code, it would look something like this:

local Pet = script.Parent
local Move = script.Parent:WaitForChild('Move')

Move.OnServerEvent:connect(function(Player)
    if workspace[Player.Name] then
        local Head = workspace[Player.Name]:FindFirstChild('Head')
        if Head then
            Pet:SetPrimaryPartCFrame(CFrame.new(Head.CFrame.X + 1,Head.CFrame.Y +                                           2,Head.CFrame.Z + 1))
            Pet:SetPrimaryPartCFrame(CFrame.new(Pet:GetPrimaryPartCFrame().p, Head.Position)); -- This is the changed place.
        end
    end
end)

Hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

0
Hello, thanks for help. But when I use this code it throws error "Unable to cast Vector3 to CoordinateFrame". Sougood -10 — 5y
0
Oh wait, lemme fix that. KingLoneCat 2642 — 5y
0
There, it should work now. KingLoneCat 2642 — 5y
0
It stil doesn't work. No error but the pet is rotated wrongly, and it stays in the rotation and doesnt do anything else. Sougood -10 — 5y
View all comments (5 more)
0
The pet is just frozen in wrong rotation Sougood -10 — 5y
0
That's probably happening because the PrimaryPart's Front face is messed up. Make a new part and make it's front the front of the Pet. In Models, the sides don't act similarly, so you need to make sure you make your own front. https://www.youtube.com/watch?v=aHzfAoXdeXs&list=UUluMbZrto7C1iQVKMTXLGWw&index=20 This youtube video shows you how I made my own pet system. KingLoneCat 2642 — 5y
0
Thanks very much. Sougood -10 — 5y
0
No problem. Glad to help. KingLoneCat 2642 — 5y
Ad

Answer this question