Advertisement
mr2meows

Character Follow Mouse

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