BodyGyro does not work

I am having trouble with BodyGyro. I am currently making a Tower Defense game but I cannot make the Tower face the target.

local bodyGyro = Instance.new("BodyGyro")
bodyGyro.MaxTorque = Vector3.LookAt(math.huge, math.huge, math.huge)
bodyGyro.D = 0
bodyGyro.CFrame = newTower.HumanoidRootPart.CFrame
bodyGyro.Parent = newTower.HumanoidRootPart

local targetCFrame = CFrame.new(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame
4 Likes

I guess you meant to use CFrame.LookAt

That still doesn’t work, I have already tried it.

1 Like

I watched this before, very useful How to use BodyGyros in Roblox Studio - YouTube

1 Like

I have made a tower defense test before and I found my self anchoring the towers instead and changing their primary part CFrame to look at the enemies on the x and z axis rather than using body gyros. Also body gyro is deprecated now in favor of AlignOrientation.

Models have a SetPrimaryPartCFrame (and GetPrimaryPartCFrame) function that requires the model to have a primary part to work.

dont use bodygyro. It’s deprecated.

So you don’t want a smooth rotation i guess, here’s a solution for that

newTower:SetPrimaryPartCFrame(CFrame.LookAt(newTower.PrimaryPart.Position, target.PrimaryPart.Position))
1 Like

So what would I do in this case? I am really bad with CFrames.

Do what that guy just posted above your post. It explains everything.

1 Like

It gives me an error

  Unable to cast Vector3 to CoordinateFrame

Do you have an alternative if I use BodyGyro?

What did you input into the SetPrimaryPartCFrame function?

I used the HumanoidRootPart. Should I be using something else?

HumanoidRootPart is fine. But what did you input in the SetPrimaryPartCFrame function?

1 Like

I think it was because I still had the tower unanchored. When I anchor the tower parts, most of my script breaks.

No, it’s the best option, but if you want you can tween the cframes

local TweenService = game:GetService("TweenService") 
TweenService:Create(
    workspace.Part1,
    TweenInfo.new(2), 
    {CFrame = CFrame.lookAt(workspace.Part1.Position,workspace.Part0.Position)}
):Play()

Anchor the tower and use this to make the tower look at the enemy.

local tower = workspace.tower -- the tower
local enemy = workspace.enemy -- the enemy

-- and most importantly, the look at cframe
local look = CFrame.lookAt(torso.Position, enemy.Torso.Position)
local matrix = CFrame.fromMatrix(torso.Position, -look.XVector, torso.CFrame.YVector, -look.ZVector)
tower:SetPrimaryPartCFrame(matrix)
-- if its looking in the wrong direction make the vectors positive instead of negative

If I anchor the tower I have to rescript a lot of code. I think I am going to try to keep with BodyGyros.

Can you help me fix my first problem? I want to keep using bodygyro.

BodyGyro is deprecated, do not use it for new work. Use AlignOrientation which is functionally similar.

AlignOrientation will actually make it easier for you to implement what you wanted, whereas with a BodyGyro you would have to update the CFrame each frame.