Advertisement
scriptingtales

Head Movement Roblox

Jun 3rd, 2023 (edited)
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.09 KB | None | 0 0
  1. --[ Head Follow Mouse/Camera ]-- -Script By lakastanmi ENJOY! <3
  2.  
  3. --[ Services ]:
  4.  
  5. local RunSrv = game:GetService("RunService");
  6.  
  7. local Wrks = game:GetService("Workspace");
  8. local Plrs = game:GetService("Players");
  9.  
  10. --[ Pre-Funcs ]:
  11.  
  12. local Ang = CFrame.Angles;  --[ Storing these as variables so I dont have to type them out. ]
  13. local aSin = math.asin;
  14. local aTan = math.atan;
  15.  
  16. --[ Constants ]:
  17.  
  18. local Cam = Wrks.CurrentCamera;
  19.  
  20. local Plr = Plrs.LocalPlayer;
  21. local Mouse = Plr:GetMouse();
  22. local Body = Wrks:WaitForChild(Plr.Name);
  23. local Head = Body:WaitForChild("Head");
  24. local Hum = Body:WaitForChild("Humanoid");
  25. local Core = Body:WaitForChild("HumanoidRootPart");
  26. local IsR6 = (Hum.RigType.Value==0);    --[ Checking if the player is using the new Rig or the original. ]
  27. local Trso = (IsR6 and Body:WaitForChild("Torso")) or Body:WaitForChild("UpperTorso");
  28. local Neck = (IsR6 and Trso:WaitForChild("Neck")) or Head:WaitForChild("Neck"); --[ Once we know the Rig, we know what to find. ]
  29.  
  30. local MseGuide = false; --[ Useful with tools if true; camera tracking runs smoother however. ]
  31. local Fctor = 1;    --[ 0: Negates tracking, 1: Relatively normal tracking, 2: Looks a little weird, just sayin'. ]
  32.  
  33. local OrgnC0 = Neck.C0; --[ Get the base C0 to manipulate off of. ]
  34.  
  35. --[ Setup ]:
  36.  
  37. Neck.MaxVelocity = 1/3
  38.  
  39. --[ Activation ]:
  40.  
  41. coroutine.resume(coroutine.create(function()
  42.     while RunSrv.RenderStepped:wait() and Hum:GetState().Value~=15 do   --[ Adding wait() here is the same as adding wait() later. ]
  43.         local CamCF = Cam.CoordinateFrame
  44.         --local MsePos = Mouse.Origin.p --[ Might do something with this in a future update... ]
  45.         if ((IsR6 and Body["Torso"]) or Body["UpperTorso"])~=nil and Body["Head"]~=nil then --[ Check for the Torso and Head... ]
  46.             local TrsoLV = Trso.CFrame.lookVector
  47.             local HdPos = Head.CFrame.p
  48.             if ((IsR6 and Trso["Neck"]) or Head["Neck"])~=nil then  --[ Make sure the Neck still exists. ]
  49.                 if Cam.CameraSubject:IsDescendantOf(Body) or Cam.CameraSubject:IsDescendantOf(Plr) then
  50.                     local Dist = nil;
  51.                     local Diff = nil;
  52.                     if not MseGuide then    --[ If not tracking the Mouse then get the Camera. ]
  53.                         Dist = (Head.CFrame.p-CamCF.p).magnitude
  54.                         Diff = Head.CFrame.Y-CamCF.Y
  55.                         if not IsR6 then    --[ R6 and R15 Neck rotation C0s are different; R15: X axis inverted and Z is now the Y. ]
  56.                             Neck.C0 = OrgnC0*Ang((aSin(Diff/Dist)*Fctor), -(((HdPos-CamCF.p).Unit):Cross(TrsoLV)).Y, 0)
  57.                         else    --[ R15s actually have the properly oriented Neck CFrame. ]
  58.                             Neck.C0 = OrgnC0*Ang(-(aSin(Diff/Dist)*Fctor), 0, -(((HdPos-CamCF.p).Unit):Cross(TrsoLV)).Y)
  59.                         end
  60.                     else
  61.                         local _, Point = Wrks:FindPartOnRay(Ray.new(Head.CFrame.p, Mouse.Hit.lookVector), Wrks, false, true)
  62.                         Dist = (Head.CFrame.p-Point).magnitude
  63.                         Diff = Head.CFrame.Y-Point.Y
  64.                         if not IsR6 then
  65.                             Neck.C0 = OrgnC0*Ang(-(aTan(Diff/Dist)*Fctor), (((HdPos-Point).Unit):Cross(TrsoLV)).Y*Fctor, 0)
  66.                         else
  67.                             Neck.C0 = OrgnC0*Ang((aTan(Diff/Dist)*Fctor), 0, (((HdPos-Point).Unit):Cross(TrsoLV)).Y*Fctor)
  68.                         end
  69.                     end
  70.                 end
  71.             end
  72.         end
  73.     end
  74. end))
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement