Cannon Rotation is messed up and is rotating everything

I’m trying to make the wood part stay on the ground and only turn left and right. While the cannon part follows the rotation of the wood part and rotate up and down. But when I rotate the cannon up or down it moves the wood part with it.

Everything you see in the video is welded to the wood circle part, except the wood base on the bottom. I need this cannon to move with the whole thing when unanchored.


Here’s the code
local cam = game.Workspace.CurrentCamera
local Gui = script.Parent
local RunService = game:GetService('RunService')
local CAS = game:GetService("ContextActionService")
local cannonNum = Gui.Cannon.Value
local cannonFold = workspace:WaitForChild("Cannon"..cannonNum)
local cannonGroup = cannonFold:WaitForChild("Cannon")
local turnBase = cannonGroup:WaitForChild("Turn"):WaitForChild("TurnBase")
local CannonPart = cannonGroup:WaitForChild("Cannon")
local camPart = cannonFold:WaitForChild("CamPart")

cam.CameraType = "Scriptable"

local UpValue, DownValue, RightValue, LeftValue = 0, 0, 0, 0

local function TurnUp(ActionName, InputState, InputObject)
    if InputState == Enum.UserInputState.Begin then	
		UpValue = 1
	elseif InputState == Enum.UserInputState.End then
		UpValue = 0
	end
end
local function TurnDown(ActionName, InputState, InputObject)
    if InputState == Enum.UserInputState.Begin then	
		DownValue = 1
	elseif InputState == Enum.UserInputState.End then
		DownValue = 0
	end
end
local function TurnRight(ActionName, InputState, InputObject)
    if InputState == Enum.UserInputState.Begin then	
		RightValue = 1
	elseif InputState == Enum.UserInputState.End then
		RightValue = 0
	end
end
local function TurnLeft(ActionName, InputState, InputObject)
    if InputState == Enum.UserInputState.Begin then	
		LeftValue = 1
	elseif InputState == Enum.UserInputState.End then
		LeftValue = 0
	end
end

local function onUpdate()
	--Get Direction
	local xAxis = RightValue - LeftValue
	local yAxis = UpValue - DownValue
	
	turnBase.CFrame = turnBase.CFrame * CFrame.Angles(xAxis*.01,0,0) --Left and Right
    CannonPart.CFrame = CannonPart.CFrame * CFrame.Angles(-yAxis*.01,0,0) --Up and Down

	--Update camera
	cam.CFrame = camPart.CFrame
end

CAS:BindAction("Up", TurnUp, false, Enum.KeyCode.W, Enum.KeyCode.Up)
CAS:BindAction("Down", TurnDown, false, Enum.KeyCode.S, Enum.KeyCode.Down)
CAS:BindAction("Right", TurnRight, false, Enum.KeyCode.D, Enum.KeyCode.Right)
CAS:BindAction("Left", TurnLeft, false, Enum.KeyCode.A, Enum.KeyCode.Left)

RunService:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value, onUpdate)

Edit:Added This the link to a place with the cannon.
CannonHelp.rbxl (42.1 KB)

2 Likes

If you want it to be attached such that the whole thing can be unanchored and moved, you may want to use constraints instead. In particular hinge contraints at all of the pivot points, and use its servo capabilities to set specific angles that the pieces will move to relative to one another, while still being able to all be unanchored and moved about.

3 Likes

Thanks a lot, but one problem… For the first couple seconds the part doesn’t move, even when the TargetAngle is set, then it starts to move later.
Nvm… i fixed it.