Turret looking at the wrong direction

  1. What do you want to achieve?
    I want to make the turret look at the player’s HumanoidRootPart’s orientation when the player’s nearby to the turret.

  2. What is the issue?
    When the player gets nearby the turret, The turret looks at a completely different direction.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried using CFrame.Angles & CFrame:ToOrientation & CFrame.fromOrientation, but none of them worked, Yes, but they didn’t work.

Here is the model for the turret: https://www.roblox.com/library/6201741245/devforumpost-2

    -- Issue part
	local a = CFrame.new(
		playerhumanoidrootpart.Orientation.X,
		playerhumanoidrootpart.Orientation.Y,
		playerhumanoidrootpart.Orientation.Z
	)
		
	model:SetPrimaryPartCFrame(
		model:GetPrimaryPartCFrame(),
			a,
			a,
			a
	)

The CFrame.Angles & CFrame.fromOrientation & CFrame:ToOrientation are not in the model since i have not saved them. I spent 2-3 hours trying to fix this one problem

1 Like

ROBLOX make a special article just for what you are looking for. Here is a screenshot for your part in particular:

The rest of the article can be found here

In your case, you can do:

model:SetPrimaryPartCFrame(
      model.PrimaryPart.Position,
      rootPart.Position
)
1 Like

I got an error saying

Unable to cast Vector3 to CoordinateFrame

my script is

-- script.Parent is the model
-- target is the humanoidrootpart

	script.Parent:SetPrimaryPartCFrame(
		script.Parent.PrimaryPart.Position,
		target.Position
	)

Whoops, my bad!

When setting the CFrame you need to do CFrame.new(Vector3, Vector3), as we only provided Vector3’s but not CFrame.new()

Here is what the code should look like:

script.Parent:SetPrimaryPartCFrame(CFrame.new(
      script.Parent.PrimaryPart.Position,
      target.Position
))
1 Like

Thank you so much! Really appreciated.

1 Like

Hey so, it works but the turret’s model is flipped on one of it’s axis, I don’t really know how i can flip it using Vector3 or anything, do you have any idea how i can fix this?

Hi there, sorry I didn’t see your message.

You can use CFrame.Angles to rotate CFrames.

For you, you would want to do:

script.Parent:SetPrimaryPartCFrame(CFrame.new(
      script.Parent.PrimaryPart.Position,
      target.Position
) * CFrame.Angles(
      math.rad(0), -- X
      math.rad(0), -- Y
      math.rad(0) -- Z
))

We use math.rad() because CFrame.Angles() takes radians as their arguments. math.rad() converts degrees to radians.

1 Like