Advertisement
TryHarderNoob

no name k

Dec 8th, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.67 KB | None | 0 0
  1. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  2. -- @CloneTrooper1019, 2015
  3. -- Realism 3 Client
  4. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  5.  
  6. local c = workspace.CurrentCamera
  7. local rs = game:GetService("RunService")
  8. local self = script.Parent
  9.  
  10. local char = self.Parent
  11. local charTorso = char:WaitForChild("Torso")
  12. local charHead = char:WaitForChild("Head")
  13. local root = char:WaitForChild("HumanoidRootPart")
  14. local rootJ = root:WaitForChild("RootJoint")
  15.  
  16. local lshoulder = charTorso:WaitForChild("Left Shoulder")
  17. local rshoulder = charTorso:WaitForChild("Right Shoulder")
  18. local neck = charTorso:WaitForChild("Neck")
  19.  
  20. local humanoid = char:WaitForChild("Humanoid")
  21. local updateY = self:WaitForChild("UpdateY")
  22.  
  23. local origins = {}
  24. local factors = {}
  25.  
  26. local function recordOrigins(...)
  27. for _,joint in pairs{...} do
  28. origins[joint] = { C0 = joint.C0, C1 = joint.C1 }
  29. end
  30. end
  31.  
  32. rootJ.C0 = CFrame.new(0, 0, 0, -0.95533663, -0.295520246, 0, 0, 0, 1, -0.295520246, 0.95533663, 0)
  33. neck.C0 = CFrame.new(0, 1, 0, -0.955336511, 0.295520216, 0, 0, 0, 1, 0.295520216, 0.955336511, 0)
  34.  
  35. recordOrigins(neck,lshoulder,rshoulder)
  36. factors =
  37. {
  38. [neck] = Vector3.new(-1,0,0);
  39. [lshoulder] = Vector3.new(0,0,-1);
  40. [rshoulder] = Vector3.new(0,0,1);
  41. }
  42.  
  43. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  44. -- Configuration
  45.  
  46. local config =
  47. {
  48. RelativeOrigin = CFrame.new(0,-1.5,-1);
  49. RelativeRotation = CFrame.new();
  50. }
  51.  
  52. local function onConfigAdded(child)
  53. if child:IsA("Configuration") and child.Name == "ViewmodelConfig" then
  54. local relativeOrigin = child:WaitForChild("RelativeOrigin").Value
  55. config.RelativeOrigin = CFrame.new(relativeOrigin)
  56. local relativeRotation = child:WaitForChild("RelativeRotation").Value
  57. config.RelativeRotation = CFrame.Angles(relativeRotation.X,relativeRotation.Y,relativeRotation.Z)
  58. end
  59. end
  60.  
  61. if char:FindFirstChild("ViewmodelConfig") then
  62. onConfigAdded(char.ViewmodelConfig)
  63. else
  64. char.ChildAdded:connect(onConfigAdded)
  65. end
  66.  
  67. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  68. -- View Bobbling
  69.  
  70. local waveScale = 0
  71. local scaleIncrement = 0.05
  72. local active = false
  73.  
  74. local function glideTorwards(currentValue,targetValue,rate)
  75. if currentValue < targetValue then
  76. return math.min(targetValue,currentValue + rate)
  77. elseif currentValue > targetValue then
  78. return math.max(targetValue,currentValue - rate)
  79. else
  80. return targetValue
  81. end
  82. end
  83.  
  84. local function getCamBobble()
  85. local goal = active and 0.5 or 0
  86. waveScale = glideTorwards(waveScale,goal,0.05)
  87. local t = math.cos(tick() * 7.85)
  88. return CFrame.new((t/3)*waveScale,math.abs(t/5)*waveScale,0)
  89. end
  90.  
  91. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  92. -- Build Viewmodel
  93.  
  94. local limbs = {}
  95.  
  96. local vm = Instance.new("Model")
  97. vm.Name = "ViewModel"
  98. local h = Instance.new("Humanoid",vm)
  99. h.Name = "VM"
  100.  
  101. local function newLimb(name,size)
  102. local limb = Instance.new("Part",vm)
  103. limb.FormFactor = "Custom"
  104. limb.Name = name
  105. limb.Size = size
  106. limb.Anchored = true
  107. limb.CanCollide = false
  108. if size.magnitude < 0.4 then
  109. limb.Transparency = 1
  110. end
  111. table.insert(limbs,limb)
  112. return limb
  113. end
  114.  
  115. local function newJoint(name,p0,p1)
  116. local j = Instance.new("Motor6D",p0)
  117. j.Name = name
  118. j.Part0 = p0
  119. j.Part1 = p1
  120. end
  121.  
  122. local head = newLimb("Head",Vector3.new())
  123. vm.PrimaryPart = head
  124.  
  125. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  126. -- Character Object Tracking
  127.  
  128. local blackList = {HumanoidRootPart = true; Torso = true; Head = true; Eyes = true; FakeHead = true}
  129.  
  130. local function watchParticle(realParticle,fakeParticle)
  131. local isTweaking = false
  132. local recTrans = realParticle.Transparency
  133. local cameraCon
  134.  
  135. local function setTransparency(t)
  136. isTweaking = true
  137. if type(t) == "number" then
  138. t = NumberSequence.new(t)
  139. end
  140. realParticle.Transparency = t
  141. fakeParticle.Transparency = recTrans
  142. isTweaking = false
  143. end
  144.  
  145. local function onCameraChanged(property)
  146. if property == "CoordinateFrame" then
  147. local isFirstPerson = (c.Focus.p-c.CoordinateFrame.p).magnitude < 1
  148. if isFirstPerson then
  149. setTransparency(1)
  150. else
  151. setTransparency(recTrans)
  152. end
  153. end
  154. end
  155.  
  156. onCameraChanged("CoordinateFrame")
  157.  
  158. local function onChanged(property)
  159. if not isTweaking then
  160. if property == "Transparency" then
  161. recTrans = realParticle.Transparency
  162. fakeParticle.Transparency = recTrans
  163. elseif property == "Parent" and not fakeParticle.Parent then
  164. cameraCon:disconnect()
  165. end
  166. end
  167. end
  168.  
  169. realParticle.Changed:connect(onChanged)
  170. cameraCon = c.Changed:connect(onCameraChanged)
  171. end
  172.  
  173. local BASE_GRIP = CFrame.new(0,-1,0) * CFrame.Angles(-0.5 * math.pi, 0, 0)
  174.  
  175. local function trackTool(tool)
  176. -- This is a lot, but I need to make sure I carefully track tools
  177. -- as they can be unpredictable
  178. toolEquipped = true
  179. local cons = {}
  180. local toolGuid = game:GetService("HttpService"):GenerateGUID(false)
  181. local handle = tool:WaitForChild("Handle")
  182. local repHandle = handle:clone()
  183. repHandle.Parent = vm
  184. repHandle.CanCollide = false
  185. repHandle.Anchored = true
  186.  
  187. local rightArm = vm:WaitForChild("Right Arm")
  188.  
  189. local function visualUpdate()
  190. local shouldHide = (c.Focus.p-c.CoordinateFrame.p).magnitude < 1
  191. handle.LocalTransparencyModifier = shouldHide and 1 or 0
  192. if repHandle then
  193. repHandle.CFrame = rightArm.CFrame * BASE_GRIP * tool.Grip:inverse()
  194. end
  195. end
  196.  
  197. rs:BindToRenderStep(toolGuid,201,visualUpdate)
  198.  
  199. local unequipCon
  200.  
  201. local function onUnequipped()
  202. toolEquipped = false
  203. unequipCon:disconnect()
  204. rs:UnbindFromRenderStep(toolGuid)
  205. repHandle:Destroy()
  206. end
  207.  
  208. unequipCon = tool.Unequipped:connect(onUnequipped)
  209. end
  210.  
  211. local function pullObject(child)
  212. if child:IsA("CharacterAppearance") then
  213. child:clone().Parent = vm
  214. elseif child:IsA("Tool") and child.RequiresHandle then
  215. trackTool(child)
  216. elseif child:IsA("BasePart") then
  217. if not blackList[child.Name] then
  218. if vm:FindFirstChild(child.Name) then
  219. vm[child.Name]:Destroy()
  220. end
  221. child.Archivable = true
  222. local fake = child:clone()
  223. fake.Parent = vm
  224. fake.Anchored = true
  225. fake.CanCollide = false
  226. child.AncestryChanged:connect(function ()
  227. if not child:IsDescendantOf(char) then
  228. fake:Destroy()
  229. end
  230. end)
  231. local children = {}
  232. local function registerChild(v)
  233. if not v:IsA("JointInstance") and not v:IsA("Sound") and not v:IsA("TouchTransmitter") then
  234. v.Archivable = true
  235. local fakeChild = v:clone()
  236. fakeChild.Parent = fake
  237. if fakeChild:IsA("ParticleEmitter") then
  238. watchParticle(v,fakeChild)
  239. end
  240. v.Changed:connect(function (property)
  241. if property ~= "Parent" then
  242. pcall(function ()
  243. fakeChild[property] = v[property]
  244. end)
  245. end
  246. end)
  247. children[v] = fakeChild
  248. end
  249. end
  250. local function unregisterChild(v)
  251. if children[v] then
  252. children[v]:Destroy()
  253. end
  254. end
  255. for _,v in pairs(child:GetChildren()) do
  256. registerChild(v)
  257. end
  258. child.ChildAdded:connect(registerChild)
  259. child.ChildRemoved:connect(unregisterChild)
  260. table.insert(limbs,fake)
  261. child.Changed:connect(function (property)
  262. if property ~= "Parent" and property ~= "LocalTransparencyModifier" and property ~= "Anchored" and property ~= "CanCollide" then
  263. pcall(function ()
  264. fake[property] = child[property]
  265. end)
  266. end
  267. end)
  268. end
  269. end
  270. end
  271.  
  272. for _,child in pairs(char:GetChildren()) do
  273. pullObject(child)
  274. end
  275.  
  276. char.ChildAdded:connect(pullObject)
  277.  
  278. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  279. -- Camera Code
  280.  
  281. if c:findFirstChild("ViewModel") then
  282. c.ViewModel:Destroy()
  283. end
  284.  
  285. local lastY = 0
  286. local lastUpdate = 0
  287. local player = game.Players.LocalPlayer
  288. local currentRot = CFrame.new()
  289. local abs = math.abs
  290. local min = math.min
  291. local max = math.max
  292.  
  293. local function inCutsceneMode()
  294. if player:FindFirstChild("CutsceneFlag") then
  295. return player.CutsceneFlag.Value
  296. end
  297. return false
  298. end
  299.  
  300. local function findAngleBetweenXZVectors(vec2, vec1)
  301. return math.atan2(vec1.X*vec2.Z-vec1.Z*vec2.X, vec1.X*vec2.X + vec1.Z*vec2.Z)
  302. end
  303.  
  304. local function renderUpdate()
  305. if #vm:GetChildren() < 1 then
  306. if con then
  307. con:disconnect()
  308. con = nil
  309. end
  310. return
  311. end
  312.  
  313. local cf = c.CoordinateFrame
  314. local look = cf.lookVector.Y
  315. local floored = math.floor(look/0.1) * 0.1
  316. local now = tick()
  317.  
  318. if lastY ~= floored and (now - lastUpdate) > 0.4 then
  319. lastUpdate = now
  320. lastY = floored
  321. updateY:FireServer(look)
  322. end
  323.  
  324. for joint,factor in pairs(factors) do
  325. local origin = origins[joint]
  326. local factor = factor * look
  327. local x,y,z = factor.X, factor.Y, factor.Z
  328. joint.C0 = origin.C0 * CFrame.Angles(x,y,z)
  329. end
  330.  
  331. neck.C0 = neck.C0 * CFrame.new(0,look/4 - 0.05,0.05 + math.max(0,look/10))
  332.  
  333. local speed = charTorso.Velocity.magnitude
  334. active = (speed > 1) and not charTorso.Anchored
  335.  
  336. local offset = CFrame.new()
  337. local bobble = getCamBobble()
  338. local cameraPos = c.CoordinateFrame.p
  339.  
  340. if (c.Focus.p-c.CoordinateFrame.p).magnitude <= 1 and not charTorso.Anchored then
  341. vm.Parent = c
  342.  
  343. local vmOffset = CFrame.new()
  344. if char:FindFirstChild("vmOffset") then
  345. vmOffset = char.vmOffset.Value
  346. end
  347.  
  348. for _,part in pairs(limbs) do
  349. local actual = char:FindFirstChild(part.Name)
  350. if actual then
  351. local cf = head.CFrame * charTorso.CFrame:toObjectSpace(actual.CFrame)
  352. if part.Name:find("Leg") then
  353. cf = cf * vmOffset:inverse()
  354. end
  355. part.BrickColor = actual.BrickColor
  356. part.CFrame = cf
  357. else
  358. part.Parent = nil
  359. end
  360. end
  361.  
  362. local base = (root.CFrame:toObjectSpace(charHead.CFrame)):inverse()
  363. local headRot = base - base.p
  364. local camPos = cf.p
  365. local rot = (cf - camPos)
  366. local desiredRot = rot * config.RelativeOrigin * bobble:inverse() * config.RelativeRotation * headRot * vmOffset * CFrame.new(0,-math.max(0.3,math.abs(look/2)),0)
  367. local angleBetween = findAngleBetweenXZVectors(currentRot.lookVector,rot.lookVector)
  368. currentRot = currentRot:lerp(desiredRot,abs(min(0.9,max(0.1,angleBetween))))
  369. vm:SetPrimaryPartCFrame(CFrame.new(camPos)*currentRot)
  370. else
  371. vm.Parent = nil
  372. currentRot = cf - cf.p - Vector3.new(0,1.5,0)
  373. end
  374. if active then
  375. humanoid.CameraOffset = bobble.p
  376. end
  377. end
  378.  
  379. rs:UnbindFromRenderStep("Kinematics") -- In case its bound already.
  380. rs:BindToRenderStep("Kinematics",201,renderUpdate)
  381.  
  382. local function onDied()
  383. rs:UnbindFromRenderStep("Kinematics")
  384. vm:Destroy()
  385. end
  386.  
  387.  
  388. humanoid.Died:connect(onDied)
  389. updateY.OnClientEvent:connect(function() end)
  390.  
  391. --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement