Stop Cannon From Turning Too Much

I want to stop my cannon from over turning, preferably only having about 45 degrees of rotation in each direction. I have no idea how to check the orientation of a model though

local plrs = game:WaitForChild("Players")
local plr = plrs.LocalPlayer
while plr.Character == nil do wait() end
local char = plr.Character
local hum = char.Humanoid

--Services
local UIS = game:GetService("UserInputService")
RS = game:GetService("RunService")

--Variables
local folder = game.Workspace.CannonFolder.AllParts
local Cannon = folder.Cannon
local PositionStart = folder.StartPart
local End = folder.EndPosition
local direction = End.Position - PositionStart.Position

--Functions
local function onfire()
	
	local duration = math.log(1.001 + direction.Magnitude * .01)
	local force = direction / duration + Vector3.new(0,game.Workspace.Gravity * duration * 0.5, 0)
	local clone = game.ReplicatedStorage.Models.CannonBall:Clone()
	clone.Position = PositionStart.Position
	clone.Parent =workspace
	clone:ApplyImpulse(force * clone.AssemblyMass)
	print("ran")
	
end

-- Camera
local camera = workspace.CurrentCamera

--[[
RS:BindToRenderStep("LockCamera",201, function()
	local CannonCFrame = folder.CFramePart
	camera.CameraType = Enum.CameraType.Scriptable
	camera.CFrame = CannonCFrame.CFrame * CFrame.new(0,2,0)

end) ]]--

--UIS Variables
local holdingRight = false
local holdingLeft = false


UIS.InputBegan:Connect(function(input)
	
	
		if input.KeyCode == Enum.KeyCode.E then
		holdingRight = true
		
		while holdingRight == true do
		print(holdingRight)
		task.wait()
		local currentCF = folder:GetPivot()
		local newCF = currentCF * CFrame.Angles(0, math.rad(.4), 0) 
		folder:PivotTo(newCF)
		end
		
		end
		
	if input.KeyCode == Enum.KeyCode.Q then
		holdingLeft = true

		while holdingLeft == true do
			folder.EndPosition.Position += Vector3.new(0,.03,0)
			task.wait()
			local currentCF = folder:GetPivot()
			local newCF = currentCF * CFrame.Angles(0, math.rad(-.4), 0)
			folder:PivotTo(newCF)
		end

	end
	
	
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		onfire()
	end
		
end)

UIS.InputEnded:Connect(function(input)

	if input.KeyCode == Enum.KeyCode.E  then
		holdingRight = false
	end

	if input.KeyCode == Enum.KeyCode.Q  then
		holdingLeft = false
	end
end)
1 Like
local OriginalOrientation = Cannon.Orientation
local MaxRotation = 45

while (OriginalOrientation-Cannon.Orientation).Magnitude <= MaxRotation do
--rotate
end

2 Likes

But how do I know if my orientation is left or right, when they’re stored the same?

maybe use .Orientation.Z).Magnitude <= MaxRotation then negate it for the right
.Orientation.Z).Magnitude <= -MaxRotation

1 Like

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