Advertisement
Catsss

HeadTurn Script

May 7th, 2018
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. --[[
  2. [Head/Waist Follow Mouse/Camera Script.]
  3. [Works with both R6 and R15, lets you turn your character's head and waist towards your mouse/camera.]
  4. [Scripted by Churningsouls
  5.  
  6.  
  7. [Anyways, here's a list of what I've added.]
  8. [Waist rotation. (Previously, only the head turned.)]
  9. [Tweening. (Basically, animating the rotation instead of instantly turning.)]
  10. [Full body rotation. (If set to true, rotates the entire body towards the mouse.)]
  11. [Specific rotation limits. (The original script used one variable to set the limits of both horizontal and vertical rotation, now there's variables for both limits!)]
  12. --]]
  13.  
  14. wait()
  15.  
  16. --[Pre-Funcs]:
  17.  
  18. local Ang = CFrame.Angles --[Storing these as variables so I dont have to type them out.]
  19. local aSin = math.asin
  20. local aTan = math.atan
  21.  
  22. --[Constants]:
  23.  
  24. local Cam = game.Workspace.CurrentCamera
  25.  
  26. local Plr = game.Players.LocalPlayer
  27. local Mouse = Plr:GetMouse()
  28. local Body = Plr.Character or Plr.CharacterAdded:wait()
  29. local Head = Body:WaitForChild("Head")
  30. local Hum = Body:WaitForChild("Humanoid")
  31. local Core = Body:WaitForChild("HumanoidRootPart")
  32. local IsR6 = (Hum.RigType.Value==0) --[Checking if the player is using R15 or R6.]
  33. local Trso = (IsR6 and Body:WaitForChild("Torso")) or Body:WaitForChild("UpperTorso")
  34. local Neck = (IsR6 and Trso:WaitForChild("Neck")) or Head:WaitForChild("Neck") --[Once we know the Rig, we know what to find.]
  35. local Waist = (not IsR6 and Trso:WaitForChild("Waist")) --[R6 doesn't have a waist joint, unfortunately.]
  36.  
  37. --[[
  38. [Whether rotation follows the camera or the mouse.]
  39. [Useful with tools if true, but camera tracking runs smoother.]
  40. --]]
  41. local MseGuide = false
  42. --[[
  43. [Whether the whole character turns to face the mouse.]
  44. [If set to true, MseGuide will be set to true and both HeadHorFactor and BodyHorFactor will be set to 0]
  45. --]]
  46. local TurnCharacterToMouse = false
  47. --[[
  48. [Horizontal and Vertical limits for head and body tracking.]
  49. [Setting to 0 negates tracking, setting to 1 is normal tracking, and setting to anything higher than 1 goes past real life head/body rotation capabilities.]
  50. --]]
  51. local HeadHorFactor = 1
  52. local HeadVertFactor = 0.6
  53. local BodyHorFactor = 0.5
  54. local BodyVertFactor = 0.4
  55.  
  56. --[[
  57. [How fast the body rotates.]
  58. [Setting to 0 negates tracking, and setting to 1 is instant rotation. 0.5 is a nice in-between that works with MseGuide on or off.]
  59. [Setting this any higher than 1 causes weird glitchy shaking occasionally.]
  60. --]]
  61. local UpdateSpeed = 0.5
  62.  
  63. local NeckOrgnC0 = Neck.C0 --[Get the base C0 to manipulate off of.]
  64. local WaistOrgnC0 = (not IsR6 and Waist.C0) --[Get the base C0 to manipulate off of.]
  65.  
  66. --[Setup]:
  67.  
  68. Neck.MaxVelocity = 1/3
  69.  
  70. -- Activation]:
  71. if TurnCharacterToMouse == true then
  72. MseGuide = true
  73. HeadHorFactor = 0
  74. BodyHorFactor = 0
  75. end
  76.  
  77. game:GetService("RunService").RenderStepped:Connect(function()
  78. local CamCF = Cam.CoordinateFrame
  79. if ((IsR6 and Body["Torso"]) or Body["UpperTorso"])~=nil and Body["Head"]~=nil then --[Check for the Torso and Head...]
  80. local TrsoLV = Trso.CFrame.lookVector
  81. local HdPos = Head.CFrame.p
  82. if IsR6 and Neck or Neck and Waist then --[Make sure the Neck still exists.]
  83. if Cam.CameraSubject:IsDescendantOf(Body) or Cam.CameraSubject:IsDescendantOf(Plr) then
  84. local Dist = nil;
  85. local Diff = nil;
  86. if not MseGuide then --[If not tracking the Mouse then get the Camera.]
  87. Dist = (Head.CFrame.p-CamCF.p).magnitude
  88. Diff = Head.CFrame.Y-CamCF.Y
  89. if not IsR6 then --[R6 and R15 Neck rotation C0s are different; R15: X axis inverted and Z is now the Y.]
  90. Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Ang((aSin(Diff/Dist)*HeadVertFactor), -(((HdPos-CamCF.p).Unit):Cross(TrsoLV)).Y*HeadHorFactor, 0), UpdateSpeed/2)
  91. Waist.C0 = Waist.C0:lerp(WaistOrgnC0*Ang((aSin(Diff/Dist)*BodyVertFactor), -(((HdPos-CamCF.p).Unit):Cross(TrsoLV)).Y*BodyHorFactor, 0), UpdateSpeed/2)
  92. else --[R15s actually have the properly oriented Neck CFrame.]
  93. Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Ang(-(aSin(Diff/Dist)*HeadVertFactor), 0, -(((HdPos-CamCF.p).Unit):Cross(TrsoLV)).Y*HeadHorFactor),UpdateSpeed/2)
  94. end
  95. else
  96. local Point = Mouse.Hit.p
  97. Dist = (Head.CFrame.p-Point).magnitude
  98. Diff = Head.CFrame.Y-Point.Y
  99. if not IsR6 then
  100. Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Ang(-(aTan(Diff/Dist)*HeadVertFactor), (((HdPos-Point).Unit):Cross(TrsoLV)).Y*HeadHorFactor, 0), UpdateSpeed/2)
  101. Waist.C0 = Waist.C0:lerp(WaistOrgnC0*Ang(-(aTan(Diff/Dist)*BodyVertFactor), (((HdPos-Point).Unit):Cross(TrsoLV)).Y*BodyHorFactor, 0), UpdateSpeed/2)
  102. else
  103. Neck.C0 = Neck.C0:lerp(NeckOrgnC0*Ang((aTan(Diff/Dist)*HeadVertFactor), 0, (((HdPos-Point).Unit):Cross(TrsoLV)).Y*HeadHorFactor), UpdateSpeed/2)
  104. end
  105. end
  106. end
  107. end
  108. end
  109. if TurnCharacterToMouse == true then
  110. Hum.AutoRotate = false
  111. Core.CFrame = Core.CFrame:lerp(CFrame.new(Core.Position, Vector3.new(Mouse.Hit.p.x, Core.Position.Y, Mouse.Hit.p.z)), UpdateSpeed / 2)
  112. else
  113. Hum.AutoRotate = true
  114. end
  115. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement