Advertisement
Guest User

VR Script real hands

a guest
Mar 4th, 2020
36,870
-1
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.88 KB | None | 0 1
  1. -- vr script by LeviTheOtaku
  2.  
  3. loadstring(game:GetObjects("rbxassetid://4480871791")[1].Source)()
  4.  
  5.  
  6.  
  7. local VRService = game:GetService("VRService")
  8.  
  9. local character = workspace.CloneCharacter
  10.  
  11. local HeadDisplay = Instance.new("Part", workspace)
  12. HeadDisplay.Anchored = true
  13. HeadDisplay.CanCollide = false
  14. HeadDisplay.Size = Vector3.new(0.5,0.5,0.5)
  15. HeadDisplay.Transparency = 1
  16. HeadDisplay.Color = Color3.fromRGB(125,125,255)
  17. HeadDisplay.TopSurface = Enum.SurfaceType.Smooth
  18. HeadDisplay.BottomSurface = Enum.SurfaceType.Smooth
  19. HeadDisplay.Name = "HeadDisplay"
  20.  
  21.  
  22. local leftHandDisplay = Instance.new("Part", workspace)
  23. leftHandDisplay.Anchored = true
  24. leftHandDisplay.CanCollide = false
  25. leftHandDisplay.Size = Vector3.new(0.5,0.5,0.5)
  26. leftHandDisplay.Transparency = 1
  27. leftHandDisplay.Color = Color3.fromRGB(125,125,255)
  28. leftHandDisplay.TopSurface = Enum.SurfaceType.Smooth
  29. leftHandDisplay.BottomSurface = Enum.SurfaceType.Smooth
  30. leftHandDisplay.Name = "leftHandDisplay"
  31.  
  32. local rightHandDisplay = Instance.new("Part", workspace)
  33. rightHandDisplay.Anchored = true
  34. rightHandDisplay.CanCollide = false
  35. rightHandDisplay.Size = Vector3.new(0.5,0.5,0.5)
  36. rightHandDisplay.Transparency = 1
  37. rightHandDisplay.Color = Color3.fromRGB(255,125,125)
  38. rightHandDisplay.TopSurface = Enum.SurfaceType.Smooth
  39. rightHandDisplay.BottomSurface = Enum.SurfaceType.Smooth
  40. rightHandDisplay.Name = "rightHandDisplay"
  41.  
  42. for i,v in next, workspace.CloneCharacter:GetChildren() do
  43. if v:IsA('Accessory') then
  44. v:Destroy()
  45. end
  46. end
  47.  
  48. spawn(function()
  49. VRService.UserCFrameChanged:Connect(function()
  50. local character = workspace.CloneCharacter
  51.  
  52. HeadScale = 1
  53. Cam = workspace.CurrentCamera
  54. Cam.HeadScale = HeadScale
  55.  
  56. local cfRH = VRService:GetUserCFrame(Enum.UserCFrame.RightHand)
  57. local RightHandCFrame = (Cam.CFrame*CFrame.new(cfRH.p*HeadScale))*CFrame.fromEulerAnglesXYZ(cfRH:ToEulerAnglesXYZ())
  58. rightHandDisplay.CFrame = RightHandCFrame
  59.  
  60. local cfLH = VRService:GetUserCFrame(Enum.UserCFrame.LeftHand)
  61. local LeftHandCFrame = (Cam.CFrame*CFrame.new(cfLH.p*HeadScale))*CFrame.fromEulerAnglesXYZ(cfLH:ToEulerAnglesXYZ())
  62. leftHandDisplay.CFrame = LeftHandCFrame
  63.  
  64. local cfH = VRService:GetUserCFrame(Enum.UserCFrame.Head)
  65. local HeadCFrame = (Cam.CFrame*CFrame.new(cfH.p*HeadScale))*CFrame.fromEulerAnglesXYZ(cfH:ToEulerAnglesXYZ())
  66. HeadDisplay.CFrame = HeadCFrame
  67.  
  68.  
  69. character["Head"].Orientation = HeadDisplay.Orientation
  70. character["Head"].Position = HeadDisplay.Position
  71. character["Left Arm"].Orientation = leftHandDisplay.Orientation + Vector3.new(45,0,0)
  72. character["Left Arm"].Position = leftHandDisplay.Position
  73. character["Right Arm"].Orientation = rightHandDisplay.Orientation + Vector3.new(45,0,0)
  74. character["Right Arm"].Position = rightHandDisplay.Position
  75. end)
  76. end)
  77.  
  78. spawn(function()
  79. ------------------------------------------------------------------------
  80. -- Freecam
  81. -- Cinematic free camera for spectating and video production.
  82. ------------------------------------------------------------------------
  83.  
  84. local pi = math.pi
  85. local abs = math.abs
  86. local clamp = math.clamp
  87. local exp = math.exp
  88. local rad = math.rad
  89. local sign = math.sign
  90. local sqrt = math.sqrt
  91. local tan = math.tan
  92.  
  93. local ContextActionService = game:GetService("ContextActionService")
  94. local Players = game:GetService("Players")
  95. local RunService = game:GetService("RunService")
  96. local StarterGui = game:GetService("StarterGui")
  97. local UserInputService = game:GetService("UserInputService")
  98. local Workspace = game:GetService("Workspace")
  99.  
  100. local LocalPlayer = Players.LocalPlayer
  101. if not LocalPlayer then
  102. Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
  103. LocalPlayer = Players.LocalPlayer
  104. end
  105.  
  106. local Camera = Workspace.CurrentCamera
  107. Workspace:GetPropertyChangedSignal("CurrentCamera"):Connect(function()
  108. local newCamera = Workspace.CurrentCamera
  109. if newCamera then
  110. Camera = newCamera
  111. end
  112. end)
  113.  
  114. ------------------------------------------------------------------------
  115.  
  116. local TOGGLE_INPUT_PRIORITY = Enum.ContextActionPriority.Low.Value
  117. local INPUT_PRIORITY = Enum.ContextActionPriority.High.Value
  118. local FREECAM_MACRO_KB = {}
  119.  
  120. local NAV_GAIN = Vector3.new(1, 1, 1)*64
  121. local PAN_GAIN = Vector2.new(0.75, 1)*8
  122. local FOV_GAIN = 300
  123.  
  124. local PITCH_LIMIT = rad(90)
  125.  
  126. local VEL_STIFFNESS = 1.5
  127. local PAN_STIFFNESS = 1.0
  128. local FOV_STIFFNESS = 4.0
  129.  
  130. ------------------------------------------------------------------------
  131.  
  132. local Spring = {} do
  133. Spring.__index = Spring
  134.  
  135. function Spring.new(freq, pos)
  136. local self = setmetatable({}, Spring)
  137. self.f = freq
  138. self.p = pos
  139. self.v = pos*0
  140. return self
  141. end
  142.  
  143. function Spring:Update(dt, goal)
  144. local f = self.f*2*pi
  145. local p0 = self.p
  146. local v0 = self.v
  147.  
  148. local offset = goal - p0
  149. local decay = exp(-f*dt)
  150.  
  151. local p1 = goal + (v0*dt - offset*(f*dt + 1))*decay
  152. local v1 = (f*dt*(offset*f - v0) + v0)*decay
  153.  
  154. self.p = p1
  155. self.v = v1
  156.  
  157. return p1
  158. end
  159.  
  160. function Spring:Reset(pos)
  161. self.p = pos
  162. self.v = pos*0
  163. end
  164. end
  165.  
  166. ------------------------------------------------------------------------
  167.  
  168. local cameraPos = Vector3.new()
  169. local cameraRot = Vector2.new()
  170. local cameraFov = 0
  171.  
  172. local velSpring = Spring.new(VEL_STIFFNESS, Vector3.new())
  173. local panSpring = Spring.new(PAN_STIFFNESS, Vector2.new())
  174. local fovSpring = Spring.new(FOV_STIFFNESS, 0)
  175.  
  176. ------------------------------------------------------------------------
  177.  
  178. local Input = {} do
  179. local thumbstickCurve do
  180. local K_CURVATURE = 2.0
  181. local K_DEADZONE = 0.15
  182.  
  183. local function fCurve(x)
  184. return (exp(K_CURVATURE*x) - 1)/(exp(K_CURVATURE) - 1)
  185. end
  186.  
  187. local function fDeadzone(x)
  188. return fCurve((x - K_DEADZONE)/(1 - K_DEADZONE))
  189. end
  190.  
  191. function thumbstickCurve(x)
  192. return sign(x)*clamp(fDeadzone(abs(x)), 0, 1)
  193. end
  194. end
  195.  
  196. local gamepad = {
  197. ButtonX = 0,
  198. ButtonY = 0,
  199. DPadDown = 0,
  200. DPadUp = 0,
  201. ButtonL2 = 0,
  202. ButtonR2 = 0,
  203. Thumbstick1 = Vector2.new(),
  204. Thumbstick2 = Vector2.new(),
  205. }
  206.  
  207. local keyboard = {
  208. W = 0,
  209. A = 0,
  210. S = 0,
  211. D = 0,
  212. E = 0,
  213. Q = 0,
  214. U = 0,
  215. H = 0,
  216. J = 0,
  217. K = 0,
  218. I = 0,
  219. Y = 0,
  220. Up = 0,
  221. Down = 0,
  222. LeftShift = 0,
  223. RightShift = 0,
  224. }
  225.  
  226. local mouse = {
  227. Delta = Vector2.new(),
  228. MouseWheel = 0,
  229. }
  230.  
  231. local NAV_GAMEPAD_SPEED = Vector3.new(1, 1, 1)
  232. local NAV_KEYBOARD_SPEED = Vector3.new(1, 1, 1)
  233. local PAN_MOUSE_SPEED = Vector2.new(1, 1)*(pi/64)
  234. local PAN_GAMEPAD_SPEED = Vector2.new(1, 1)*(pi/8)
  235. local FOV_WHEEL_SPEED = 1.0
  236. local FOV_GAMEPAD_SPEED = 0.25
  237. local NAV_ADJ_SPEED = 0.75
  238. local NAV_SHIFT_MUL = 0.25
  239.  
  240. local navSpeed = 1
  241.  
  242. function Input.Vel(dt)
  243. navSpeed = clamp(navSpeed + dt*(keyboard.Up - keyboard.Down)*NAV_ADJ_SPEED, 0.01, 4)
  244.  
  245. local kGamepad = Vector3.new(
  246. thumbstickCurve(gamepad.Thumbstick1.x),
  247. thumbstickCurve(gamepad.ButtonR2) - thumbstickCurve(gamepad.ButtonL2),
  248. thumbstickCurve(-gamepad.Thumbstick1.y)
  249. )*NAV_GAMEPAD_SPEED
  250.  
  251. local kKeyboard = Vector3.new(
  252. keyboard.D - keyboard.A + keyboard.K - keyboard.H,
  253. keyboard.E - keyboard.Q + keyboard.I - keyboard.Y,
  254. keyboard.S - keyboard.W + keyboard.J - keyboard.U
  255. )*NAV_KEYBOARD_SPEED
  256.  
  257. local shift = UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) or UserInputService:IsKeyDown(Enum.KeyCode.RightShift)
  258.  
  259. return (kGamepad + kKeyboard)*(navSpeed*(shift and NAV_SHIFT_MUL or 1))
  260. end
  261.  
  262. function Input.Pan(dt)
  263. local kGamepad = Vector2.new(
  264. thumbstickCurve(gamepad.Thumbstick2.y),
  265. thumbstickCurve(-gamepad.Thumbstick2.x)
  266. )*PAN_GAMEPAD_SPEED
  267. local kMouse = mouse.Delta*PAN_MOUSE_SPEED
  268. mouse.Delta = Vector2.new()
  269. return kGamepad + kMouse
  270. end
  271.  
  272. function Input.Fov(dt)
  273. local kGamepad = (gamepad.ButtonX - gamepad.ButtonY)*FOV_GAMEPAD_SPEED
  274. local kMouse = mouse.MouseWheel*FOV_WHEEL_SPEED
  275. mouse.MouseWheel = 0
  276. return kGamepad + kMouse
  277. end
  278.  
  279. do
  280. local function Keypress(action, state, input)
  281. keyboard[input.KeyCode.Name] = state == Enum.UserInputState.Begin and 1 or 0
  282. return Enum.ContextActionResult.Sink
  283. end
  284.  
  285. local function GpButton(action, state, input)
  286. gamepad[input.KeyCode.Name] = state == Enum.UserInputState.Begin and 1 or 0
  287. return Enum.ContextActionResult.Sink
  288. end
  289.  
  290. local function MousePan(action, state, input)
  291. local delta = input.Delta
  292. mouse.Delta = Vector2.new(-delta.y, -delta.x)
  293. return Enum.ContextActionResult.Sink
  294. end
  295.  
  296. local function Thumb(action, state, input)
  297. gamepad[input.KeyCode.Name] = input.Position
  298. return Enum.ContextActionResult.Sink
  299. end
  300.  
  301. local function Trigger(action, state, input)
  302. gamepad[input.KeyCode.Name] = input.Position.z
  303. return Enum.ContextActionResult.Sink
  304. end
  305.  
  306. local function MouseWheel(action, state, input)
  307. mouse[input.UserInputType.Name] = -input.Position.z
  308. return Enum.ContextActionResult.Sink
  309. end
  310.  
  311. local function Zero(t)
  312. for k, v in pairs(t) do
  313. t[k] = v*0
  314. end
  315. end
  316.  
  317. function Input.StartCapture()
  318. ContextActionService:BindActionAtPriority("FreecamKeyboard", Keypress, false, INPUT_PRIORITY,
  319. Enum.KeyCode.W, Enum.KeyCode.U,
  320. Enum.KeyCode.A, Enum.KeyCode.H,
  321. Enum.KeyCode.S, Enum.KeyCode.J,
  322. Enum.KeyCode.D, Enum.KeyCode.K,
  323. Enum.KeyCode.E, Enum.KeyCode.I,
  324. Enum.KeyCode.Q, Enum.KeyCode.Y,
  325. Enum.KeyCode.Up, Enum.KeyCode.Down
  326. )
  327. ContextActionService:BindActionAtPriority("FreecamMousePan", MousePan, false, INPUT_PRIORITY, Enum.UserInputType.MouseMovement)
  328. ContextActionService:BindActionAtPriority("FreecamMouseWheel", MouseWheel, false, INPUT_PRIORITY, Enum.UserInputType.MouseWheel)
  329. ContextActionService:BindActionAtPriority("FreecamGamepadButton", GpButton, false, INPUT_PRIORITY, Enum.KeyCode.ButtonX, Enum.KeyCode.ButtonY)
  330. ContextActionService:BindActionAtPriority("FreecamGamepadTrigger", Trigger, false, INPUT_PRIORITY, Enum.KeyCode.ButtonR2, Enum.KeyCode.ButtonL2)
  331. ContextActionService:BindActionAtPriority("FreecamGamepadThumbstick", Thumb, false, INPUT_PRIORITY, Enum.KeyCode.Thumbstick1, Enum.KeyCode.Thumbstick2)
  332. end
  333.  
  334. function Input.StopCapture()
  335. navSpeed = 1
  336. Zero(gamepad)
  337. Zero(keyboard)
  338. Zero(mouse)
  339. ContextActionService:UnbindAction("FreecamKeyboard")
  340. ContextActionService:UnbindAction("FreecamMousePan")
  341. ContextActionService:UnbindAction("FreecamMouseWheel")
  342. ContextActionService:UnbindAction("FreecamGamepadButton")
  343. ContextActionService:UnbindAction("FreecamGamepadTrigger")
  344. ContextActionService:UnbindAction("FreecamGamepadThumbstick")
  345. end
  346. end
  347. end
  348.  
  349. local function GetFocusDistance(cameraFrame)
  350. local znear = 0.1
  351. local viewport = Camera.ViewportSize
  352. local projy = 2*tan(cameraFov/2)
  353. local projx = viewport.x/viewport.y*projy
  354. local fx = cameraFrame.rightVector
  355. local fy = cameraFrame.upVector
  356. local fz = cameraFrame.lookVector
  357.  
  358. local minVect = Vector3.new()
  359. local minDist = 512
  360.  
  361. for x = 0, 1, 0.5 do
  362. for y = 0, 1, 0.5 do
  363. local cx = (x - 0.5)*projx
  364. local cy = (y - 0.5)*projy
  365. local offset = fx*cx - fy*cy + fz
  366. local origin = cameraFrame.p + offset*znear
  367. local _, hit = Workspace:FindPartOnRay(Ray.new(origin, offset.unit*minDist))
  368. local dist = (hit - origin).magnitude
  369. if minDist > dist then
  370. minDist = dist
  371. minVect = offset.unit
  372. end
  373. end
  374. end
  375.  
  376. return fz:Dot(minVect)*minDist
  377. end
  378.  
  379. ------------------------------------------------------------------------
  380.  
  381. local function StepFreecam(dt)
  382. local vel = velSpring:Update(dt, Input.Vel(dt))
  383. local pan = panSpring:Update(dt, Input.Pan(dt))
  384. local fov = fovSpring:Update(dt, Input.Fov(dt))
  385.  
  386. local zoomFactor = sqrt(tan(rad(70/2))/tan(rad(cameraFov/2)))
  387.  
  388. cameraFov = clamp(cameraFov + fov*FOV_GAIN*(dt/zoomFactor), 1, 120)
  389. cameraRot = cameraRot + pan*PAN_GAIN*(dt/zoomFactor)
  390. cameraRot = Vector2.new(clamp(cameraRot.x, -PITCH_LIMIT, PITCH_LIMIT), cameraRot.y%(2*pi))
  391.  
  392. local cameraCFrame = CFrame.new(cameraPos)*CFrame.fromOrientation(cameraRot.x, cameraRot.y, 0)*CFrame.new(vel*NAV_GAIN*dt)
  393. cameraPos = cameraCFrame.p
  394.  
  395. Camera.CFrame = cameraCFrame
  396. Camera.Focus = cameraCFrame*CFrame.new(0, 0, -GetFocusDistance(cameraCFrame))
  397. Camera.FieldOfView = cameraFov
  398. end
  399.  
  400. ------------------------------------------------------------------------
  401.  
  402. local PlayerState = {} do
  403. local mouseBehavior
  404. local cameraType
  405. local cameraFocus
  406. local cameraCFrame
  407. local cameraFieldOfView
  408. local screenGuis = {}
  409. local coreGuis = {
  410. Backpack = true,
  411. Chat = true,
  412. Health = true,
  413. PlayerList = true,
  414. }
  415. local setCores = {
  416. BadgesNotificationsActive = true,
  417. PointsNotificationsActive = true,
  418. }
  419.  
  420. -- Save state and set up for freecam
  421. function PlayerState.Push()
  422. for name in pairs(coreGuis) do
  423. coreGuis[name] = StarterGui:GetCoreGuiEnabled(Enum.CoreGuiType[name])
  424. StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[name], false)
  425. end
  426. for name in pairs(setCores) do
  427. setCores[name] = StarterGui:GetCore(name)
  428. StarterGui:SetCore(name, false)
  429. end
  430. local playergui = LocalPlayer:FindFirstChildOfClass("PlayerGui")
  431. if playergui then
  432. for _, gui in pairs(playergui:GetChildren()) do
  433. if gui:IsA("ScreenGui") and gui.Enabled then
  434. screenGuis[#screenGuis + 1] = gui
  435. gui.Enabled = false
  436. end
  437. end
  438. end
  439.  
  440. cameraFieldOfView = Camera.FieldOfView
  441. Camera.FieldOfView = 70
  442.  
  443. cameraType = Camera.CameraType
  444. Camera.CameraType = Enum.CameraType.Custom
  445.  
  446. cameraCFrame = Camera.CFrame
  447. cameraFocus = Camera.Focus
  448.  
  449. mouseBehavior = UserInputService.MouseBehavior
  450. UserInputService.MouseBehavior = Enum.MouseBehavior.Default
  451. end
  452.  
  453. -- Restore state
  454. function PlayerState.Pop()
  455. for name, isEnabled in pairs(coreGuis) do
  456. StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType[name], isEnabled)
  457. end
  458. for name, isEnabled in pairs(setCores) do
  459. StarterGui:SetCore(name, isEnabled)
  460. end
  461. for _, gui in pairs(screenGuis) do
  462. if gui.Parent then
  463. gui.Enabled = true
  464. end
  465. end
  466.  
  467. Camera.FieldOfView = cameraFieldOfView
  468. cameraFieldOfView = nil
  469.  
  470. Camera.CameraType = cameraType
  471. cameraType = nil
  472.  
  473. Camera.CFrame = cameraCFrame
  474. cameraCFrame = nil
  475.  
  476. Camera.Focus = cameraFocus
  477. cameraFocus = nil
  478.  
  479. UserInputService.MouseBehavior = mouseBehavior
  480. mouseBehavior = nil
  481. end
  482. end
  483.  
  484. local function StartFreecam()
  485. local cameraCFrame = Camera.CFrame
  486. cameraRot = Vector2.new(cameraCFrame:toEulerAnglesYXZ())
  487. cameraPos = cameraCFrame.p
  488. cameraFov = Camera.FieldOfView
  489.  
  490. velSpring:Reset(Vector3.new())
  491. panSpring:Reset(Vector2.new())
  492. fovSpring:Reset(0)
  493.  
  494. PlayerState.Push()
  495. RunService:BindToRenderStep("Freecam", Enum.RenderPriority.Camera.Value, StepFreecam)
  496. Input.StartCapture()
  497. end
  498.  
  499. local function StopFreecam()
  500. Input.StopCapture()
  501. RunService:UnbindFromRenderStep("Freecam")
  502. PlayerState.Pop()
  503. end
  504.  
  505. ------------------------------------------------------------------------
  506.  
  507. do
  508. local enabled = true
  509. StartFreecam()
  510.  
  511. local function ToggleFreecam()
  512. if enabled then
  513. StopFreecam()
  514. else
  515. StartFreecam()
  516. end
  517. enabled = not enabled
  518. end
  519.  
  520. local function CheckMacro(macro)
  521. for i = 1, #macro - 1 do
  522. if not UserInputService:IsKeyDown(macro[i]) then
  523. return
  524. end
  525. end
  526. ToggleFreecam()
  527. end
  528.  
  529. local function HandleActivationInput(action, state, input)
  530. if state == Enum.UserInputState.Begin then
  531. if input.KeyCode == FREECAM_MACRO_KB[#FREECAM_MACRO_KB] then
  532. CheckMacro(FREECAM_MACRO_KB)
  533. end
  534. end
  535. return Enum.ContextActionResult.Pass
  536. end
  537.  
  538. ContextActionService:BindActionAtPriority("FreecamToggle", HandleActivationInput, false, TOGGLE_INPUT_PRIORITY, FREECAM_MACRO_KB[#FREECAM_MACRO_KB])
  539. end
  540. end)
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement