Need help with rotating turret's barrel like in armored patrol

I need help with making my turret rotate in the like in armored patrol

  1. I want to make an armored patrol turret that rotates on mouse click like this

  1. I dont know how to make the turret’s barrel rotate in the y axis

  1. *I couldn’t make the turret rotate in the y axis
-- Turret Code
local mouse = game.Players.LocalPlayer:GetMouse()

local BaseModel = game.Workspace.Base
local turret = BaseModel.Turret
local Base = BaseModel.Mount
local weld = Base.Weld


local turretOffset = weld.C0

mouse.Button1Down:Connect(function()
	local turretCF = Base.CFrame * turretOffset
	local Pos = turretCF:PointToObjectSpace(mouse.Hit.p)
	local Math = math.atan2(-Pos.x, -Pos.z)
    
	weld.C0 = turretOffset * CFrame.fromEulerAnglesYXZ(0, Math, 0)
	
end)
1 Like

There are many ways to do it, one is like your angle way.

Personally I use CFrame.lookAt then convert it into orientation then constraint and clamp the orientation. This allows me to control the rotation along the Y axis which is yaw left and right (look left and right), and the X axis which controls the pitch (look up and down).

I want ONLY the barrel to rotate in the y axis and i dont know how to set up the module

I believe you meant X Axis rotation.

Because rotation along the y axis refers to this, which is what your turret is already doing:

The rule is known as the right hand rule and is commonly used within physics for stuff like angular velocity and torque. The convention is anticlockwise along the +Y axis when looking down from a birds eye view.

image

I believe you mean rotation up and down the Y axis, or rotation along the turrets local X axis So it should be something like this.

-- Turret Code
local mouse = game.Players.LocalPlayer:GetMouse()

local BaseModel = game.Workspace.Base
local turret = BaseModel.Turret
local Base = BaseModel.Mount
local weld = Base.Weld


local turretOffset = weld.C0

mouse.Button1Down:Connect(function()
	local turretCF = Base.CFrame * turretOffset
	local Pos = turretCF:PointToObjectSpace(mouse.Hit.p)
	local verticalAngle = math.atan2(Pos.Y, -Pos.z)
    
	weld.C0 = turretOffset * CFrame.fromEulerAnglesYXZ(verticalAngle, 0, 0)
	
end)

Similar to this

BTW, my module also has a place file you can download from the GitHub page so you can play around with it.

3 Likes