SHARE
TWEET

Pointing to a Position

wackeyhd5cool Apr 19th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local Inst_player = game.Players.LocalPlayer;
  2. local Inst_mouse = Inst_player:GetMouse();
  3. repeat wait() until Inst_player.Character;
  4. local Inst_character = Inst_player.Character;
  5.  
  6. local function onMove(Inst_weld)
  7.     --Inst_weld: the weld created when the tool gets equiped
  8.    
  9.     local Vec_position = Inst_mouse.Hit.p;
  10.     local Vec_globalC0Position = (Inst_weld.Part0.CFrame*Inst_weld.C0).p;
  11.     local CF_pointingCF = CFrame.new(Vec_globalC0Position,Vec_position);
  12.     local CF_pointingCFWithoutPos = CF_pointingCF - CF_pointingCF.p;
  13.        
  14.     Inst_weld.C0 = CFrame.new(Inst_weld.C0.p)*((Inst_weld.Part0.CFrame-Inst_weld.Part0.CFrame.p):inverse())*CF_pointingCFWithoutPos
  15.    
  16.     --[[
  17.             Vec_position is where the mouse points to, in the game (ie. 0,3,4)
  18.             Vec_globalC0Position is the global position of the C0 derived by mutliplying the C0 onto the Part0 CFrame and
  19.             taking its position
  20.             CF_pointingCF is a cframe pointing from the the C0's global position to where the mouse pointed
  21.             CF_pointingCFWithoutPos is the position of CF_pointingCF
  22.            
  23.             ====           
  24.            
  25.             Now, to get a CFrame which makes the arm point to a position is this:
  26.             The >first< thing I tried to do was:
  27.            
  28.                 CFrame.new(Inst_weld.C0.p)*CF_pointingCFWithoutPos (there wasn't the inverse CFrame)
  29.                
  30.             It did not work because CFrame.new(Inst_weld.C0.p) ALWAYS has a lookvector as if you're facing forwards; so,
  31.             when I moved my character rotation-wise (ie. turn 45 degrees), this line of code spit out a cframe assuming that
  32.             I was facing forwards. So, b/c this is a weld and b/c of that erroneous CFrame, my arm moved more than it should have.
  33.              
  34.             We needed some way to counteract this rotation and give a proper one. So I thought, if rotating moved it too much,
  35.             then an inverse of the rotation would balance it. I came up with:
  36.            
  37.                 ((Inst_weld.Part0.CFrame-Inst_weld.Part0.CFrame.p):inverse())
  38.                
  39.        
  40.         --]]
  41.    
  42. end
  43.  
  44. script.Parent.Equipped:Connect(function()
  45.    
  46.     local Inst_weld = Instance.new("Weld");
  47.     Inst_weld.Part0 = Inst_character.Torso;
  48.     Inst_weld.Part1 = Inst_character["Right Arm"];
  49.     Inst_weld.C0 = CFrame.new(1.5,0,0);
  50.     Inst_weld.C1 = CFrame.Angles(math.rad(-90),0,0)*CFrame.new(0,-.5,.5);
  51.     Inst_weld.Parent = Inst_character.Torso;
  52.  
  53.    
  54.     Inst_mouse.Move:Connect(function()
  55.         onMove(Inst_weld)
  56.     end)
  57.    
  58.     --[[
  59.         Analysis:
  60.        
  61.         First, create weld.
  62.          Set the torso as the Part0 and the Right Arm as the Part1
  63.          Set the C0; this manner makes the arm where it would be without the weld (character's resting state)
  64.          Set the C1; this manner >first< rotates the arm and then moves it.
  65.             Recall that if we set the C1 position first (CFrame.new(0,-.5,1.5) it'll rotate around the offset point, which isn't
  66.             its own position
  67.          Parent the weld as the torso
  68.        
  69.         When the mouse moves, call onMove with argument as the weld
  70.        
  71.        
  72.     --]]
  73.    
  74. end)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top