-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFirstPersonScript.lua
82 lines (68 loc) · 2.78 KB
/
FirstPersonScript.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
local LicencingTable = {
Creator = "RoomGENERAT0R",
Description = "To create a first person body resembling real life.",
Uses = "Interminable Rooms",
ScriptVersion = "6",
Configuration = "Default"
} -- Licencing table for RoomGENERAT0R's publicized scripts. This table does not interfere with the script.
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Camera = workspace.CurrentCamera
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")
local Settings = {
FirstPersonDistanceThreshold = 1,
CameraOffsetYClamp = 0.5,
DefaultCameraOffset = Vector3.new(0, 0, 0),
FirstPersonCameraOffset = Vector3.new(0, 0, -1),
FieldOfView = 70,
SmoothTransition = true,
TransitionSpeed = 0.1
}
local function IsInFirstPerson()
if not Camera or not Head then return false end
local Distance = (Camera.CFrame.Position - Head.Position).Magnitude
return Distance < Settings.FirstPersonDistanceThreshold
end
LocalPlayer.CharacterAdded:Connect(function(NewCharacter)
Character = NewCharacter
Humanoid = Character:WaitForChild("Humanoid")
Head = Character:WaitForChild("Head")
for _, BodyPart in pairs(Character:GetChildren()) do
if BodyPart:IsA("BasePart") and BodyPart.Name ~= "Head" then
BodyPart:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
BodyPart.LocalTransparencyModifier = BodyPart.Transparency
end)
BodyPart.LocalTransparencyModifier = BodyPart.Transparency
end
end
end)
for _, BodyPart in pairs(Character:GetChildren()) do
if BodyPart:IsA("BasePart") and BodyPart.Name ~= "Head" then
BodyPart:GetPropertyChangedSignal("LocalTransparencyModifier"):Connect(function()
BodyPart.LocalTransparencyModifier = BodyPart.Transparency
end)
BodyPart.LocalTransparencyModifier = BodyPart.Transparency
end
end
local CurrentCameraOffset = Settings.DefaultCameraOffset
RunService.RenderStepped:Connect(function()
if not Humanoid or not Camera then return end
Camera.FieldOfView = Settings.FieldOfView
local TargetOffset
if IsInFirstPerson() then
local LookVector = Camera.CFrame.LookVector
local ClampedY = math.clamp(LookVector.Y, -Settings.CameraOffsetYClamp, Settings.CameraOffsetYClamp)
TargetOffset = Vector3.new(Settings.FirstPersonCameraOffset.X, ClampedY, Settings.FirstPersonCameraOffset.Z)
else
TargetOffset = Settings.DefaultCameraOffset
end
if Settings.SmoothTransition then
CurrentCameraOffset = CurrentCameraOffset:Lerp(TargetOffset, Settings.TransitionSpeed)
else
CurrentCameraOffset = TargetOffset
end
Humanoid.CameraOffset = CurrentCameraOffset
end)