Advertisement
GForcebit

Realistic walk/camera script

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