Advertisement
BobMe

Untitled

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