How to limit CFrame rotation on a specific axis

I am trying to make a cannon/part face towards the player’s mouse, but only on the y axis. It can currently turn a full 360 degrees. I want to make it so that it has limited turning.

How would I go about this?

local RNS = game:GetService("RunService")
local PS = game:GetService("Players")

local player = PS.LocalPlayer
local Mouse = player:GetMouse()
local part = workspace:WaitForChild("Cannon"):WaitForChild("Cannon")

RNS.RenderStepped:Connect(function()
	local pos = Vector3.new(Mouse.Hit.Position.X, part.Position.Y, Mouse.Hit.Position.Z)
	part.CFrame = CFrame.new(part.Position, pos)
end)
1 Like

I haven’t tested this but maybe try setting the x and z axis to what it is is set to while not running or whatever, so that is would still detect and change the y axis (again I’m not in studio so I’m not sure if this will work)

I ended up figuring it out on my own, I just need to check to max sure the Y orientation wasn’t greater than -135 and also was less than 135. And if it was, it would just set the orientation to Vector3.new(0, -135, 0)

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