Help with ViewportFrame

I have an issue with my script.

Whenever I pick up a gun from workspace, for some reason it’s like this:

image

Basically, It’s tilted wrong.

(I’m also not the best at CFrames, sorry!)

Script:

local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tool = script.Parent
local GlockModule = Tool:WaitForChild("GlockModule"):Clone()
local Handle = Tool:WaitForChild("Handle")
local ToolName = Tool:WaitForChild("ToolName")
local LocalPlayer = game.Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Animator = Character:WaitForChild("Humanoid"):WaitForChild("Animator")
local Mouse = LocalPlayer:GetMouse()
local CurrentCamera = workspace.CurrentCamera
local Modules = ReplicatedStorage:WaitForChild("Modules")
local StrafingController = require(Modules:WaitForChild("StrafingController"))
local ScreenGui = LocalPlayer.PlayerGui:WaitForChild("ScreenGui")
local ItemEquipFrame = ScreenGui:WaitForChild("ItemEquipFrame")


local SFXFolder = Handle
local EquipSFX = SFXFolder:WaitForChild("Equip")
local ShootSFX = SFXFolder:WaitForChild("Shoot")
local UnequipSFX = SFXFolder:WaitForChild("Unequip")
local ZoomSFX = SFXFolder:WaitForChild("Zoom")


local AnimationsFolder = Tool:WaitForChild("Animations")
local IdleAnim = AnimationsFolder:WaitForChild("Idle")
local ZoomAnim = AnimationsFolder:WaitForChild("Zoom")

-- Settings

local TweenInfo = TweenInfo.new(.25, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut)
local NormalFOV = 70
local ZoomFOV = 50
local GunEquipped = false

-- Loaded Animations

local LoadedIdleAnim = Animator:LoadAnimation(IdleAnim)
local LoadedZoomAnim = Animator:LoadAnimation(ZoomAnim)

Mouse.Button2Down:Connect(function()
	if GunEquipped == true then
		ZoomSFX:Play()
		LoadedZoomAnim:Play()
		local CamTween = TweenService:Create(CurrentCamera,TweenInfo,{FieldOfView = ZoomFOV})
		CamTween:Play()
	end
end)

Mouse.Button2Up:Connect(function()
	if GunEquipped == true then
		LoadedZoomAnim:Stop()
		local CamTween = TweenService:Create(CurrentCamera,TweenInfo,{FieldOfView = NormalFOV})
		CamTween:Play()
	end
end)

Tool.Equipped:Connect(function()
	GunEquipped = true
	Mouse.Icon = "rbxassetid://10397909758"
	EquipSFX:Play()
	LoadedIdleAnim:Play()
	ScreenGui:WaitForChild("ItemEquipFrame").Visible = true
	local Camera = Instance.new("Camera"); Camera.Parent = ScreenGui:WaitForChild("ItemEquipFrame"):WaitForChild("ViewportFrame")
	ScreenGui:WaitForChild("ItemEquipFrame"):WaitForChild("ViewportFrame").CurrentCamera = Camera
	ScreenGui:WaitForChild("ItemEquipFrame"):WaitForChild("ItemLabel").Text = ToolName.Value
	GlockModule.Parent = ScreenGui:WaitForChild("ItemEquipFrame"):WaitForChild("ViewportFrame")
	Camera.CFrame = GlockModule.CFrame * CFrame.new(1,0,0) -- I assume this or the line below it is the problem, but how would I go on to solve this?
	Camera.CFrame = CFrame.new(Camera.CFrame.Position, GlockModule.Position)
	StrafingController:SetEnabled(true)
end)

Tool.Unequipped:Connect(function()
	GunEquipped = false
	Mouse.Icon = ""
	UnequipSFX:Play()
	LoadedIdleAnim:Stop()
	LoadedZoomAnim:Stop()
	CurrentCamera.FieldOfView = NormalFOV
	StrafingController:SetEnabled(false)
	if ScreenGui:WaitForChild("ItemEquipFrame"):WaitForChild("ViewportFrame").Camera then
		ScreenGui:WaitForChild("ItemEquipFrame").Visible = false
		ScreenGui:WaitForChild("ItemEquipFrame"):WaitForChild("ViewportFrame").GlockModule.Parent = nil
		ScreenGui:WaitForChild("ItemEquipFrame"):WaitForChild("ViewportFrame").Camera:Destroy()
	end
end)

If you ever accidentally put a viewportframe in the workspace then anything inside it will start simulating physics. I had this exact issue, so make sure that isn’t happening.

I don’t think the viewport is in the workspace, it’s in a ScreenGui.

That is fine. I commend you for using full names for your variables. I see UserInputService replaced with UIS because people are lazy.

Yes you are completely correct this would be the problem.

The issue is quite simple really you are not recentering your “GlockModule” when you load it into the viewport frame.

What I recommend is after you have cloned your Part you need to reset the CFrame. (Basically reseting it to 0,0,0 with no rotation)

GlockModule.CFrame = CFrame.new()
Also instead of having 2 lines to set the camera CFrame you can use
Camera.CFrame = CFrame.lookAt(Vector3.xAxis*CAMERA_DISTANCE,Vector3.zero)
You can substitute you camera distance from how far you need your camera to be.
Optionally the third parameter for CFrame.lookAt allows you to specify the “Up Direction”. So if you do not want to move the GlockModule you can get the upVector from the GlockModule and do something like

Camera.CFrame = CFrame.lookAt(Vector3.xAxis*CAMERA_DISTANCE,Vector3.zero,GlockModule.CFrame.UpVector)

This would assume that your GlockModule is at 0,0,0

Your code is very neat! Well done!

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