How do I ignore the Y value on CFrame.lookAt?

How do I ignore the Y value on CFrame.lookAt?
I want to let my pet follow and rotate to the player’s HumanoidRootPart. But the pet is also looking at the HumanoidRootPart Y value.
Here’s the script:

function RotatePet(Pet)
      local Pet.XR.Value = math.random(-9, 9) -- How much will the pet rotates (X)
      local Pet.ZR.Value = math.random(-9, 9) -- How much will the pet rotates (Z)
      Pet.PrimaryPart.BodyGyro.CFrame = CFrame.lookAt(Pet.PrimaryPart.Position, 
      (Pet.Owner.Value.HumanoidRootPart.Position + Vector3.new(Pet.XR.Value, 0, Pet.ZR.Value)))
end

Any solution will be appreciated :+1:

2 Likes

Make the Y component of the looked at vector be equal to the pet’s Y position. This means constructing the HumanoidRootPart position vector without the Y component - that is, Pet.Owner.Value.HumanoidRootPart.Position * Vector3.new(1, 0, 1) or Vector3.new(Pet.Owner.Value.HumanoidRootPart.Position.X, 0, Pet.Owner.Value.HumanoidRootPart.Position.Z) - and then adding Vector3.new(0, Pet.PrimaryPart.Position.Y, 0).

2 Likes

Do you mean the X value (up and down), not Y (left and right)?

local look = CFrame.new(Pet.PrimaryPart.Position, Pet.Owner.Value.PrimaryPart.Position)
local x,y,z = look:ToOrientation()
Pet.PrimaryPart.BodyGyro.CFrame = CFrame.new(look.Position) 
    * CFrame.Angles(0,y,z)
3 Likes

How the X value is supposed to be up and down?

2 Likes

When you rotate an object, the X axis circle (red) is turning it up and down relative to the object.

2 Likes

Oh yeah, you were right. So why are you using CFrame.new instead of CFrame.lookAt?

2 Likes

Using CFrame.new(Vector3, Vector3) will do the same thing. I prefer this because .lookAt is ugly.

3 Likes

Ok, I’ll try the CFrame.new one.

2 Likes

Edit: Thanks it works perfectly.

2 Likes