Gun camera only half working

The gun will work sometimes when you look straight ahead. However when you look up or down or to the sides it gets all wierd. Im trying everything and it isnt working.

https://gyazo.com/efcfc2478208208500962e38c955d344

function TweenRotation(FunctCamera)
	local Part1 = Panel.PrimaryPart
	local TweenService = game:GetService("TweenService")
local PanelRoot = game.Workspace.TrackerPart
	local x, y, z = FunctCamera.CFrame:toEulerAnglesXYZ()

	local PanelSwingInfo = TweenInfo.new(0.1) -- Let's use all defaults here
	
	
		local root = game.Players.LocalPlayer.Character.HumanoidRootPart
	local direction = root.CFrame:ToObjectSpace(Camera.CFrame).lookVector

	local PanelSwingTween = TweenService:Create(PanelRoot, PanelSwingInfo, {
		
		
		--CFrame = CFrame.Angles(math.asin(direction.Y),-math.asin(direction.X),math.asin(direction.Z))
		
	--	CFrame = CFrame.Angles(math.asin(direction.Y),-math.asin(direction.X), math.asin(direction.Z))
		
		CFrame = Camera.CFrame
		--CFrame = CFrame.new(PanelRoot.Position) * CFrame.Angles(x, y, z)
	
		--Orientation = Vector3.new(math.deg(x), math.deg(y), math.deg(z))
	})

	PanelSwingTween:Play()
end

	
	game:GetService("RunService").RenderStepped:Connect(function()

	--Panel:SetPrimaryPartCFrame(CFrame.new(Camera.CFrame.Position, game.Workspace.TrackerPart.Orientation))
	Panel:SetPrimaryPartCFrame(CFrame.new(Camera.CFrame.Position) * CFrame.Angles(math.rad(game.Workspace.TrackerPart.Orientation.X), math.rad(game.Workspace.TrackerPart.Orientation.Y), math.rad(game.Workspace.TrackerPart.Orientation.Z)))

	print(game.Workspace.TrackerPart.Orientation)
	if num == 1 then
		TweenRotation(Camera)
		num = 0
	end
	num = num + 1
end)

I believe the issue is this:

Whoa that’s a really long line. Yeah thing is CFrame.Angles does not apply rotations in the same order as how rotations are applied with orientation seen with the documentation below:

CFrame.Angles ( number rx, number ry, number rz )
Equivalent to fromEulerAnglesXYZ

CFrame.fromOrientation ( number rx, number ry, number rz )
Equivalent to fromEulerAnglesYXZ

A better way to obtain a CFrame with the same orientation as that workspace tracker part is to do this by simply getting its CFrame without the position

local trackerPart = game.Workspace.TrackerPart
local cameraPositionCFOnly = CFrame.new(Camera.CFrame.Position)
local rotationCFOfTrackerPartOnly = trackerPart.CFrame - trackerPart.CFrame.Position
local goalCFrame = cameraPositionCFOnly*rotationCFOfTrackerPartOnly
Panel:SetPrimaryPartCFrame(goalCFrame)

Otherwise yeah please use variables…

I want the gun to also have a slight offset. So it has a sort of drift effect similar to this. So i have a tween that handles rotation and the setprimarypart is for position.

Have you tried out the solution? The solution I posted above should set the goalCFrame Orientation to the same as the part that is being tweened so if you want an offset you should offset that par that is being tweened.

OOOHHHH it makes sense now thank you!

wait(3)
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local Zoomed = false
local Zooming = false
local trackerPart = game.Workspace.TrackerPart
local AlreadyZooming = false
CamPart = game.ReplicatedStorage.FAMAS:Clone()
CamPart.Parent = workspace
	local Camera = game.Workspace.CurrentCamera
local Panel = game.Workspace.FAMAS
local Part1 = Panel.PrimaryPart
local PanelRoot = Panel.PrimaryPart
local num = 0
for _, Part0 in pairs(Panel:GetChildren()) do
	if Part0:IsA("BasePart") and not (Part0 == Part1) then
		local WeldConstraint = Instance.new("WeldConstraint")
		WeldConstraint.Part0 = Part0
		WeldConstraint.Part1 = Part1
		WeldConstraint.Parent = WeldConstraint.Part0

		Part0.Anchored = false
	end
end

mouse.Button2Down:Connect(function()

Player.Character.Humanoid.WalkSpeed = 8
Panel.PrimaryPart = Panel.CamPart2
	local PanelRoot = Panel.CamPart2
end)

mouse.Button2Up:Connect(function()

Player.Character.Humanoid.WalkSpeed = 16
Panel.PrimaryPart = Panel.CamPart
	local PanelRoot = Panel.CamPart
end)

function TweenRotation(FunctCamera)
	local Part1 = Panel.PrimaryPart
	local TweenService = game:GetService("TweenService")
local PanelRoot = game.Workspace.TrackerPart
	local x, y, z = FunctCamera.CFrame:toEulerAnglesXYZ()

	local PanelSwingInfo = TweenInfo.new(0.1) -- Let's use all defaults here
	
	

	local PanelSwingTween = TweenService:Create(PanelRoot, PanelSwingInfo, {
		
		CFrame = Camera.CFrame
	
	})

	PanelSwingTween:Play()
end

	
	game:GetService("RunService").RenderStepped:Connect(function()
	local trackerPart = game.Workspace.TrackerPart
	local cameraPositionCFOnly = CFrame.new(Camera.CFrame.Position)
	local rotationCFOfTrackerPartOnly = trackerPart.CFrame - trackerPart.CFrame.Position
	local goalCFrame = cameraPositionCFOnly*rotationCFOfTrackerPartOnly
	Panel:SetPrimaryPartCFrame(goalCFrame)
	
	
	print(game.Workspace.TrackerPart.Orientation)
	if num == 1 then
		TweenRotation(Camera)
		num = 0
	end
	num = num + 1
end)


here is the uncleaned gun code since you helped make it!

1 Like