|
| 23 Nov 2017 06:48 PM |
I'm trying to write a script where a model, with the primary part anchored, always points toward my head, but when I try to alter the orientation of the primary part, the welds break on the model. I hear changing the CFrame can fix this, but I don't know how to change the orientation by CFrame.
Here is the script:
char = game.Workspace:WaitForChild("RaichuaTheFurry") axis = script.Parent.Axis
while wait() do local charPosX, charPosY, charPosZ = char.Head.Position.X, char.Head.Position.Y, char.Head.Position.Z local zyAdj = charPosZ - axis.Position.Z local xOpp, yOpp = charPosX - axis.Position.X, charPosY - axis.Position.Y local xHyp_yAdj = math.sqrt(zyAdj^2 + xOpp^2) if zyAdj < 0 then xHyp_yAdj = -xHyp_yAdj end local xATanDeg = (180 * (math.atan(xOpp / zyAdj))) / math.pi local yATanDeg = (180 * (math.atan(yOpp / xHyp_yAdj))) / math.pi if zyAdj ~= 0 then axis.Orientation = Vector3.new(axis.Orientation.X, xATanDeg, axis.Orientation.Z) if yOpp ~= 0 then axis.Orientation = Vector3.new(-yATanDeg, axis.Orientation.Y, axis.Orientation.Z) else axis.Orientation = Vector3.new(0, axis.Orientation.Y, axis.Orientation.Z) end else if yOpp > 0 then axis.Orientation = Vector3.new(-90, axis.Orientation.Y, axis.Orientation.Z) elseif yOpp < 0 then axis.Orientation = Vector3.new(90, axis.Orientation.Y, axis.Orientation.Z) else axis.Orientation = Vector3.new(0, axis.Orientation.Y, axis.Orientation.Z) end end end |
|
|
| Report Abuse |
|
|
|
| 23 Nov 2017 06:52 PM |
| You can changed the orientation of a part through CFrame in multiple ways. One is by defining where you want the part to face by doing this: part1.CFrame = CFrame.new(part1.Position, part2.Position) --This will make part1 face part2 another way to go about this is by: part1.CFrame = part1.CFrame * CFrame.Angles(math.rad(15),math.rad(15),math.rad(15)) -- This will make ##### #### ## ##### and rotate to the radians given. |
|
|
| Report Abuse |
|
|
|
| 23 Nov 2017 06:56 PM |
| So I basically had to relearn basic trig today just to find out that there's already a built-in pointing function? :P |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 23 Nov 2017 06:57 PM |
| Nice math, though... Much better than I could do.. Or event think of XD |
|
|
| Report Abuse |
|
|
| |
|