So I have this code which will make sure the part stays within range and adds handles to it however whenever the part is at the edge and I switch axis it starts spinning randomly out of control. Any help on how I would stop this would be appreciated
Video:
Explanation.wmv (685.1 KB)
Code:
local Uis = game:GetService('UserInputService')
local Axiss = "Y"
local LastC = script.Parent.CFrame
local Colors = {["Y"] = Color3.fromRGB(0, 179, 1),["X"] = Color3.fromRGB(185, 6, 9),["Z"] = Color3.fromRGB(0, 0, 179)}
local Holding = false
local Updated = true
local InRange = true
local function GetActualAxis(axis)
local Look = "Z"
local Right = "X"
local Up = "Y"
local Lv = math.abs(script.Parent.CFrame.LookVector[axis])
local Rv = math.abs(script.Parent.CFrame.RightVector[axis])
local Uv = math.abs(script.Parent.CFrame.UpVector[axis])
if Lv > Rv and Lv > Uv then
return Look,Vector3.new(0,0,1),Vector3.new(0,-90,0)
end
if Rv > Lv and Rv > Uv then
return Right,Vector3.new(1,0,0),Vector3.new(0,0,0)
end
if Uv > Lv and Uv > Rv then
return Up,Vector3.new(0,1,0),Vector3.new(0,0,90)
end
return Look,Vector3.new(0,0,1),Vector3.new(0,-90,0)
end
local function UpdateHandles()
if game.Workspace:FindFirstChild('h1') then
game.Workspace["h1"]:Destroy()
end
if game.Workspace:FindFirstChild('h2') then
game.Workspace["h2"]:Destroy()
end
local Handle1 = game.ReplicatedStorage.Handle:Clone()
local Handle2 = game.ReplicatedStorage.Handle:Clone()
Handle1.Name = "h1"
Handle2.Name = "h2"
Handle1:PivotTo(script.Parent.CFrame)
Handle2:PivotTo(script.Parent.CFrame)
--Handle2:PivotTo(CFrame.new(Handle2:GetPivot().Position) * CFrame.lookAt(Handle2:GetPivot().Position,script.Parent.Position).Rotation:Inverse())
Handle1.Top.Color = Colors[Axiss]
Handle1.Handle.Color = Colors[Axiss]
Handle2.Top.Color = Colors[Axiss]
Handle2.Handle.Color = Colors[Axiss]
if Axiss == "Z" then
Handle1:PivotTo(Handle1:GetPivot() * CFrame.Angles(math.rad(90),0,0))
Handle2:PivotTo(Handle2:GetPivot() * CFrame.Angles(math.rad(-90),0,0))
elseif Axiss == "X" then
Handle1:PivotTo(Handle1:GetPivot() * CFrame.Angles(0,math.rad(0),math.rad(-90)))
Handle2:PivotTo(Handle2:GetPivot() * CFrame.Angles(0,math.rad(180),math.rad(-90)))
elseif Axiss == "Y" then
Handle1:PivotTo(Handle1:GetPivot() * CFrame.Angles(0,0,0))
Handle2:PivotTo(Handle2:GetPivot() * CFrame.Angles(0,0,math.rad(180)))
end
Handle1:PivotTo(Handle1:GetPivot() + (Handle1.Handle.CFrame.UpVector * Vector3.new(Handle1:GetExtentsSize().Y,Handle1:GetExtentsSize().Y,Handle1:GetExtentsSize().Y)))
Handle2:PivotTo(Handle2:GetPivot() + (Handle2.Handle.CFrame.UpVector * Vector3.new(Handle2:GetExtentsSize().Y,Handle2:GetExtentsSize().Y,Handle2:GetExtentsSize().Y)))
script.Parent.H1.Part0 = Handle1.PrimaryPart
script.Parent.H2.Part0 = Handle2.PrimaryPart
Handle1.Parent = workspace
Handle2.Parent = workspace
end
local function ChangeAxis()
local Axis,Vector,Rot = GetActualAxis(Axiss)
script.Parent.DragDetector.Axis = Vector
script.Parent.DragDetector.Orientation = Rot
end
local StartP = script.Parent.Position
Uis.InputBegan:Connect(function(input,processed)
if processed == true and Holding == false then
return
end
if input.KeyCode == Enum.KeyCode.E then
if Axiss == "Y" then
Axiss = "Z"
elseif Axiss == "Z" then
Axiss = "X"
elseif Axiss == "X" then
Axiss = "Y"
end
ChangeAxis()
script.Parent.DragDetector:RestartDrag()
LastMotion = CFrame.new(0,0,0)
script.Parent.DragDetector.DragFrame = script.Parent.DragDetector.DragFrame.Rotation * CFrame.new(0,0,0)
if Holding == true then
StartP = script.Parent.Position
UpdateHandles()
end
end
end)
ChangeAxis()
script.Parent.DragDetector.DragStart:Connect(function()
StartP = script.Parent.Position
LastMotion = CFrame.new(0,0,0)
script.Parent.Highlight.Enabled = true
script.Parent.DragDetector.MaxForce = ((script.Parent:GetMass() + 1.05) * game.Workspace.Gravity) * 150
LastC = script.Parent.CFrame
Holding = true
ChangeAxis()
UpdateHandles()
end)
script.Parent.DragDetector:AddConstraintFunction(2,function(motion)
if (((StartP + script.Parent.Size * Vector3.FromAxis(Enum.Axis[Axiss])/2) + (motion.Position * Vector3.FromAxis(Enum.Axis[Axiss]))) - game.Players.LocalPlayer.Character.PrimaryPart.Position).Magnitude > 32 or (((StartP - script.Parent.Size * Vector3.FromAxis(Enum.Axis[Axiss])/2) + (motion.Position * Vector3.FromAxis(Enum.Axis[Axiss]))) - game.Players.LocalPlayer.Character.PrimaryPart.Position).Magnitude > 32 then
print("resetting")
return LastMotion
end
LastMotion = motion
end)
script.Parent.DragDetector.DragEnd:Connect(function()
StartP = script.Parent.Position
script.Parent.Highlight.Enabled = false
Holding = false
LastMotion = CFrame.new(0,0,0)
LastC = script.Parent.CFrame
if game.Workspace:FindFirstChild('h1') then
game.Workspace["h1"]:Destroy()
end
if game.Workspace:FindFirstChild('h2') then
game.Workspace["h2"]:Destroy()
end
Updated = true
end)