SimTek

Find XY Angle Between 2 Hrps

Sep 14th, 2021 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.79 KB | None | 0 0
  1. -- Angle the nextHrp is based on the hrp's LookVector
  2. local function XZAngleToNextHrp(hrp, nextHrp)
  3.     -- Vector pointing in the direction of the nextHrp of length 1
  4.     local dir = (nextHrp.Position - hrp.Position).Unit
  5.    
  6.     -- We only want to find the angle to the right or left, not up and down
  7.     -- Get rid of the Y value
  8.     local vecA = Vector2.new(dir.X, dir.Z)
  9.     local vecB = Vector2.new(hrp.CFrame.LookVector.X, hrp.CFrame.LookVector.Z)
  10.  
  11.    
  12.     -- Dot product is used to find the angle between lookVec and dir
  13.     local dotValue = vecA:Dot(vecB)
  14.    
  15.     -- Cross product creates another vector used to orient the +/- of the angle
  16.     local crossValue = vecA:Cross(vecB)
  17.    
  18.     -- with the dot product and cross product, atan2 find the angle you need
  19.     local angle = math.atan2(crossValue, dotValue)
  20.     return angle
  21. end
Add Comment
Please, Sign In to add comment