Advertisement
Guest User

vr script

a guest
Jan 14th, 2020
21,901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. local InputService = game:GetService("UserInputService")
  2. local Camera = game.Workspace.CurrentCamera
  3. local Player = game.Players.LocalPlayer
  4. local Character = Player.Character
  5. local Head = Character.Head
  6. local Torso = Character.Torso
  7. local RootPart = Character.HumanoidRootPart
  8. local RootJoint = RootPart.RootJoint
  9. local Neck = Torso.Neck
  10. Camera.FieldOfView = 90
  11. Camera.CameraType = "Scriptable"
  12. InputService.MouseBehavior = Enum.MouseBehavior.LockCenter
  13.  
  14.  
  15.  
  16. local v3 = Vector3.new
  17. local cf = CFrame.new
  18. local components = cf().components
  19. local inverse = cf().inverse
  20. local fromAxisAngle = CFrame.fromAxisAngle
  21. local atan,atan2 = math.atan,math.atan2
  22. local acos = math.acos
  23.  
  24. local function toAxisAngleFromVector(v)
  25. local z = v.z
  26. return z*z<0.99999 and v3(v.y,-v.x,0).unit*acos(-z) or v3()
  27. end
  28.  
  29. local function AxisAngleLookOrientation(c,v,t)
  30. local c = c-c.p
  31. local rv = (inverse(c)*v).unit
  32. local rz = rv.z
  33. return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
  34. end
  35.  
  36. local function AxisAngleLookNew(v,t)--CFrame,Vector,Tween
  37. local rv = v.unit
  38. local rz = rv.z
  39. return rz*rz<0.99999 and fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or cf()
  40. end
  41.  
  42. local function AxisAngleLook(c,v,t)--CFrame,Vector,Tween
  43. local rv = (inverse(c)*v).unit
  44. local rz = rv.z
  45. return rz*rz<0.99999 and c*fromAxisAngle(v3(rv.y,-rv.x,0),acos(-rz)*(t or 1)) or c
  46. end
  47.  
  48.  
  49.  
  50.  
  51. local Sensitivity = 0.005
  52.  
  53.  
  54. local CameraDirection = Vector3.new(0,0,1)
  55.  
  56. local function EulerAnglesYX(l)
  57. local x,z = l.x,l.z
  58. return atan(l.y/(x*x+z*z)^0.5),-atan2(x,-z)
  59. end
  60.  
  61. local function AnglesXY(l)
  62. local z = l.z
  63. return atan2(l.y,-z),-atan2(l.x,-z)
  64. end
  65.  
  66. local function MouseMoved(Input)
  67. if Input.UserInputType == Enum.UserInputType.MouseMovement then
  68. local dx,dy = Input.Delta.x*Sensitivity,Input.Delta.y*Sensitivity
  69. local m2 = dx*dx+dy*dy
  70. if m2>0 then
  71. CameraDirection = (AxisAngleLookOrientation(RootPart.CFrame,CameraDirection)*fromAxisAngle(v3(-dy,-dx,0),m2^0.5)).lookVector
  72. end
  73. local RootOrientation = RootPart.CFrame-RootPart.Position
  74. local RelativeDirection = RootOrientation:inverse()*CameraDirection
  75. local AngX,AngY = AnglesXY(RelativeDirection)--RootOrientation:inverse()*
  76. if AngX<-1.57*11/12 then
  77. local y,z,c,s = RelativeDirection.y,RelativeDirection.z,math.cos(-1.57*11/12-AngX),-math.sin(-1.57*11/12-AngX)
  78. z,y = z*c-y*s,z*s+y*c
  79. CameraDirection = RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
  80. elseif AngX>1.57*11/12 then
  81. local y,z,c,s = RelativeDirection.y,RelativeDirection.z,math.cos(1.57*11/12-AngX),-math.sin(1.57*11/12-AngX)
  82. z,y = z*c-y*s,z*s+y*c
  83. CameraDirection = RootOrientation*v3(RelativeDirection.x<0 and -(1-y*y-z*z)^0.5 or (1-y*y-z*z)^0.5,y,z)
  84. end
  85. end
  86. end
  87.  
  88. local Mouse = Player:GetMouse()
  89.  
  90. local Zoom = -0.5
  91.  
  92. Mouse.KeyDown:connect(function(k)
  93. if k == "e" then
  94. Zoom = -0.5
  95. elseif k == "q" then
  96. Zoom = 8
  97. end
  98. end)
  99.  
  100. InputService.InputChanged:connect(MouseMoved)
  101.  
  102. Neck.C1 = cf()
  103.  
  104. local _
  105. local DirectionBound = 3.14159/3
  106. local CurrentAngY = 0
  107.  
  108. local function CameraUpdate()
  109. Camera.CameraType = "Scriptable"
  110. local cx,cz = CameraDirection.x,CameraDirection.z
  111. local rvx,rvz = RootPart.Velocity.x,RootPart.Velocity.z
  112. if rvx*rvx+rvz*rvz>4 and cx*rvx+cz*rvz<-0.5*(cx*cx+cz*cz)^0.5*(rvx*rvx+rvz*rvz)^0.5 then
  113. DirectionBound = math.min(DirectionBound*0.9,math.abs(CurrentAngY*0.9))
  114. else
  115. DirectionBound = DirectionBound*0.1+3.14159/3*0.9
  116. end
  117. local AngX,AngY = EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
  118. if AngY>DirectionBound then
  119. RootPart.CFrame = RootPart.CFrame*CFrame.Angles(0,AngY-DirectionBound,0)
  120. elseif AngY<-DirectionBound then
  121. RootPart.CFrame = RootPart.CFrame*CFrame.Angles(0,AngY+DirectionBound,0)
  122. end
  123. _,CurrentAngY = EulerAnglesYX((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection)
  124. local CameraOrientation = AxisAngleLookNew((RootPart.CFrame-RootPart.Position):inverse()*CameraDirection,1)
  125. Neck.C0 = CFrame.new(0,1,0)*CameraOrientation*CFrame.new(0,0.5,0)
  126. local PreCam = AxisAngleLook(RootPart.CFrame*cf(0,1,0),RootPart.CFrame*v3(0,1,0)+CameraDirection)*CFrame.new(0,0.825,0)
  127. if Zoom == 8 then
  128. local Part,Position = workspace:findPartOnRay(Ray.new(PreCam.p,PreCam.lookVector*-8),Character)
  129. Camera.CoordinateFrame = PreCam*CFrame.new(0,0,(Position-PreCam.p).magnitude)
  130. else
  131. Camera.CoordinateFrame = PreCam*CFrame.new(0,0,Zoom)
  132. end
  133. end
  134.  
  135. game:GetService("RunService").RenderStepped:connect(CameraUpdate)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement