How to get a turret to face a player

Turret hierarchy:
image

TurretShooty is unanchored besides the primary part which all parts are welded to

TurretMovey is all anchored including primary part

When turret movey moves so does turret shooty.

I want turretshooty to face the player’s root accordingly to its Y axis.

Heres the aiming part of the script:

-- Face target to then shoot
function FaceTarget()
	local CurrentTarget = Targets[1].Character
	local CurrentTargetHumanoid = CurrentTarget.Humanoid
	if CurrentTargetHumanoid.Health > 0 then
		local CharacterPosition = CurrentTarget.PrimaryPart.Position

		-- Calculate rotation for TurretMovey
		local NewMoveyCFrame = CFrame.lookAt(TurretMovey.PrimaryPart.Position, CharacterPosition - Vector3.new(0, -180, 0))
		local TurretMoveyCFrame = CFrame.fromMatrix(TurretMovey.PrimaryPart.Position, TurretMovey.PrimaryPart.CFrame.XVector, NewMoveyCFrame.YVector)

		-- Calculate rotation for TurretShooty

		TurretMovey:SetPrimaryPartCFrame(TurretMoveyCFrame)

		print("Adjusting")
	end
end

For a motor6d/weld CFrame method you can do this:

1 Like

ive been trying to avoid physics based coding
if no one else has got a solution ill use this, thank you for the resource

1 Like

use :ToOrientation().

example,

local TurretShootyPosition = Script.Parent.Position
local LookAtCFrame = CFrame.lookAt(TurretShootyPosition,pos2)

local PitchAngle,_,_ = LookAtCFrame:ToOrientation()
local CFrameResult = CFrame.new(TurretShootyPosition) * CFrame.Angles(PitchAngle,0,0)

If it seems to be reversed then change the PitchAngle in the CFrame.Angles() bit to -PitchAngle. And CFrameResult should be the CFrame you wanted.

1 Like

me too i dont even wanna know what it is

1 Like

okay i mean with some tweaking i could get this to work but turret move doesnt move turret shooty now

local NewMoveyCFrame = CFrame.lookAt(TurretMovey.PrimaryPart.Position, CharacterPosition - Vector3.new(0, -180, 0))

Why are you subtracting by Vector3.new(0, -180, 0) here?

1 Like

because:
(in this demo i disabled the script Korpsii provided)

1 Like
local TurretShootyPosition = Script.Parent.Position
local LookAtCFrame = CFrame.lookAt(TurretShootyPosition,pos2)

local PitchAngle,_,_ = LookAtCFrame:ToOrientation()
local CFrameResult = TurretMovey.CFrame * CFrame.Angles(PitchAngle,0,0)

try

1 Like

Hi, I’d recommend that watch these videos, it explains how to make a turret, like yours.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.