--Max/min degrees for panning: 359/0


TurnJump=1.0		--Number of degrees each movement makes. 1 = 1 degree every time; 0.5 = half a degree every time; ect.
TurnSpeed=0.0001	--Wait time between jumps
SpeedVariation=1		


Beam=script.Parent.Tilt.H1
CPan=0
CTilt=0

local Pan = Instance.new("Weld")
Pan.Part0 = script.Parent.Base
Pan.Part1 = script.Parent.Pan.Handle
Pan.C1 = CFrame.fromEulerAnglesXYZ(0, 0, 0) * CFrame.new(0,1,0)
Pan.Parent = script.Parent
local Tilt = Instance.new("Weld")
Tilt.Part0 = script.Parent.Pan.Center
Tilt.Part1 = script.Parent.Tilt.Handle
Tilt.C1 = CFrame.fromEulerAnglesXYZ(math.pi/-2, math.pi/2, 0) * CFrame.new(0,0,0)
Tilt.Parent = script.Parent

OneDegree=math.pi/180
function Degrees(Des)
	return(Des*OneDegree)
end

Colors={"Hot pink", "Really blue"}
function RandColor()
	return Colors[math.random(1, #Colors)]
end

function On(Color)
	Beam.BrickColor=BrickColor.new(Color)
	Beam.Transparency=0.3
end
function Off()
	Beam.Transparency=1
end

function Reach(PanDegree,TiltDegree,Speed)
	ReachedPan=false
	ReachedTilt=false
	Moving=true

	--Send out the directional degree spiders! They will go out and seek the shortest path around.
	Current=CPan
	Pos=0
	while Current~=PanDegree do
		Current=Current+TurnJump
		Pos=Pos+1
		if Current>359 then
			Current=Current-360
		end
	end
	Current=CPan
	Neg=0
	while Current~=PanDegree do
		Current=Current-TurnJump
		Neg=Neg+1
		if Current<0 then
			Current=Current+360
		end
	end
	Rand=math.random(1,2)	

	while ReachedPan==false or ReachedTilt==false and Moving==true do
		if CPan-0.5<PanDegree and CPan+0.5>PanDegree then
			ReachedPan=true
		end
		if CTilt-0.5<TiltDegree and CTilt+0.5>TiltDegree then
			ReachedTilt=true
		end
		if ReachedPan==false then
			PN=0					--] Deternine Direction of movement
			if Pos<Neg then			--]
				PN=1					--]
			elseif Neg<Pos then		--]
				PN=-1					--]
			elseif Neg==Pos then		--]
				if Rand==1 then			--]
					PN=-1					--]
				else						--]
					PN=1					--]
				end						--]
			end						--]

			CPan=CPan+TurnJump*PN
			if CPan>359 then
				CPan=CPan-360
			end
			if CPan<0 then
				CPan=360+CPan
			end	
			Pan.C1=Pan.C1*CFrame.fromEulerAnglesXYZ(0,Degrees(1)*PN*TurnJump,0)
		end
		if ReachedTilt==false then
			PN=0						--] Deternine Direction of movement
			if TiltDegree-CTilt<0 then		--]
				PN=-1						--]
			elseif TiltDegree-CTilt>=0 then	--]
				PN=1						--]
			end							--]
			CTilt=CTilt+PN*TurnJump
			Tilt.C1=Tilt.C1*CFrame.fromEulerAnglesXYZ(0,Degrees(1)*PN*TurnJump,0)
		end
		wait(Speed)
	end
end


function Move()
	Moving=false
	wait(script.Parent.Move.Value.Z*2)
	Reach(script.Parent.Move.Value.X,script.Parent.Move.Value.Y,script.Parent.Move.Value.Z)
end
script.Parent.Move.Changed:connect(Move)
while true do
	On(RandColor())
	Reach(math.random(0,159),math.random(-115,115),TurnSpeed+math.random(TurnSpeed*-SpeedVariation*1000000,TurnSpeed*SpeedVariation*1000000)/1000000)
end

