[Sort of advanced CFrame issues] - How can I fix this? [closed]
So i'm trying to make a telekinesis tool, it uses BodyPosition to move the selected part around.. and it works but there's a problem. I have a conditional statement, where if the mouse's hit position is further away from the specified limit then the position shouldn't go any further away. Now the strangest this is that that bit works, but with the conditional then after it goes past the limit, the position automatically makes the X axis 0 for some unknown reason. I want to know how I can make the new position match up with the mouse's X axis, but still have the Z axis limit. Any ideas? Here's the bit of code for moving the part, all variables are defined and DistanceLimit is 100.
This is a localscript.
01 | Move = ( function (Mouse, Obj) |
02 | local BodyPosition, CharTorso = Instance.new( "BodyPosition" ,Obj), Plyr.Character.Torso; |
03 | BodyPosition.maxForce = Vector 3. new(Obj.Size.X* 5000 ,Obj.Size.Y* 5000 ,Obj.Size.Z* 5000 ) |
06 | local mouseHit = Mouse.Hit.p |
09 | if (mouseHit - CharTorso.CFrame.p).magnitude < = DistanceLimit then |
10 | pos = CFrame.new(mouseHit.X, 5 ,mouseHit.Z) |
14 | pos = (CFrame.new(mouseHit.X, 5 , 0 ) + Vector 3. new(CharTorso.CFrame * CFrame.new( 0 , 0 ,DistanceLimit))) |
18 | BodyPosition.position = (pos).p |
22 | BodyPosition:Destroy() |