View difference between Paste ID: x3zVJvED and spRXaJpF
SHOW: | | - or go back to the newest paste.
1
--//Camera Script//--
2
--Services
3
local UIS = game:GetService("UserInputService")
4
local RunService = game:GetService("RunService")
5
local TweenService = game:GetService("TweenService")
6
7
--Player
8
local Player = game.Players.LocalPlayer
9
repeat wait() until Player.Character
10
repeat wait() until Player.Character.Humanoid
11
repeat wait() until Player.Character.HumanoidRootPart
12
local Character = Player.Character
13
local HumanoidRootPart = Character.HumanoidRootPart
14
local Humanoid = Character.Humanoid
15
local CamScript = Player.PlayerScripts.CameraScript
16
CamScript.Disabled = true
17
Humanoid.AutoRotate = false
18
19
--Mouse
20
local Mouse = Player:GetMouse()
21
22
--Variables
23
local Popper = true			--	Sets whether Popper Cam behaviour is enabled
24
local DeltaX = 0
25
local DeltaY = 0
26
local AngleH = 0
27
local AngleV = 0
28
local SensitivityY = 120	--	Determines how large a change in vertical angle is through a mouse rotation
29
local SensitivityX = 120	--	Determines how large a change in the horizontal angle is through a mouse rotation
30
local W = false
31
local A = false
32
local S = false
33
local D = false
34
local Offset = CFrame.new(2, 3, 15)	--	Determines the CFrame by which the camera is pushed from the CFrame of the HumanoidRootPart
35
local MaxY = 5*math.pi/12				--	Determines maximum vertical angle
36
local MinY = -5*math.pi/12			--	Determines minimum vertical angle
37
local Combinations = {
38
	{true, false, false, false, 0, 0},
39
	{true, true, false, false, math.pi/4},
40
	{false, true, false, false, math.pi/2},
41
	{false, true, true, false, 3*math.pi/4},
42
	{false, false, true, false, math.pi},
43
	{false, false, true, true, 5*math.pi/4},
44
	{false, false, false, true, 3*math.pi/2},
45
	{true, false, false, true, 7*math.pi/4}
46
}
47
48
--Camera
49
local Cam = game.Workspace.CurrentCamera
50
Cam.CameraType = Enum.CameraType.Scriptable
51
UIS.MouseBehavior = Enum.MouseBehavior.LockCenter
52
53
--Functions
54
local function CheckForPart(Target)
55
	local CheckRay = Ray.new(Target.p, (Target.p - Cam.CoordinateFrame.p).Unit)
56
	local Part, Position = game.Workspace:FindPartOnRay(CheckRay, Cam, false, true)
57
	return Part
58
end
59
60
local function Push(CamCFrame)
61
	local Directions = {
62
		CFrame.new(0, 0, -1),
63
		CFrame.new(0, 1, 0),
64
		CFrame.new(0, -1, 0),
65
		CFrame.new(1, 0, 0),
66
		CFrame.new(-1, 0, 0)
67
	}
68
	for Num, Val in pairs(Directions) do
69
		local TestCFrame = CamCFrame * Val
70
		local Part = CheckForPart(TestCFrame)
71
		if(Part == nil) then
72
			return TestCFrame
73
		end
74
	end
75
end
76
77
RunService.RenderStepped:Connect(function()
78
	AngleH = AngleH - DeltaX/SensitivityX
79
	DeltaX = 0
80
	AngleV = math.clamp(AngleV - DeltaY/SensitivityY, MinY, MaxY)
81
	DeltaY = 0
82
	local FinalCFrame = CFrame.new(HumanoidRootPart.Position) * CFrame.Angles(0, AngleH, 0) * CFrame.Angles(AngleV, 0, 0) * Offset
83
	Cam.CoordinateFrame = FinalCFrame
84
	if(Popper == true) then
85
		Cam.CoordinateFrame = Cam.CoordinateFrame * CFrame.new(0, 0, -Cam:GetLargestCutoffDistance({Character}))
86
	end
87
	if(W == true) or (A == true) or (S == true) or (D == true) then
88
		for Num, Val in pairs(Combinations) do
89
			if(Val[1] == W) and (Val[2] == A) and (Val[3] == S) and (Val[4] == D) then
90
				local DirectionVector = Cam.CoordinateFrame.lookVector
91
				local Position = HumanoidRootPart.Position
92
				local TargetCFrame = CFrame.new(HumanoidRootPart.Position, HumanoidRootPart.Position + Vector3.new(DirectionVector.X, 0, DirectionVector.Z))
93
				HumanoidRootPart.CFrame = HumanoidRootPart.CFrame:lerp(TargetCFrame * CFrame.Angles(0, Val[5], 0), 0.25)
94
			end
95
		end		
96
	end
97
end)
98
99
UIS.InputChanged:Connect(function(Input, Bool)
100
	if(Bool == false) then
101
		if(Input.UserInputType == Enum.UserInputType.MouseMovement) then
102
			if(DeltaX ~= Input.Delta.X) then
103
				DeltaX = Input.Delta.X
104
			end
105
			if(DeltaY ~= Input.Delta.Y) then
106
				DeltaY = Input.Delta.Y
107
			end
108
		end
109
	end
110
end)
111
112
UIS.InputBegan:Connect(function(Input, Bool)
113
	if(Bool == false) then
114
		if(Input.KeyCode == Enum.KeyCode.W) then
115
			W = true
116
		elseif(Input.KeyCode == Enum.KeyCode.A) then
117
			A = true
118
		elseif(Input.KeyCode == Enum.KeyCode.S) then
119
			S = true
120
		elseif(Input.KeyCode == Enum.KeyCode.D) then
121
			D = true
122
		end
123
	end
124
end)
125
126
UIS.InputEnded:Connect(function(Input, Bool)
127
	if(Bool == false) then
128
		if(Input.KeyCode == Enum.KeyCode.W) then
129
			W = false
130
		elseif(Input.KeyCode == Enum.KeyCode.A) then
131
			A = false
132
		elseif(Input.KeyCode == Enum.KeyCode.S) then
133
			S = false
134
		elseif(Input.KeyCode == Enum.KeyCode.D) then
135
			D = false
136
		end
137
	end
138
end)