--When strafing make sure you can see target from end pos

function movement.strafe()
	local goalPos = status:get("currentTarget").Position
	local origin,dir = myRoot.Position,nil
	
	local action = math.random(2)
	if action == 1 then
		dir = myRoot.CFrame.RightVector
	elseif action == 2 then
		dir = -myRoot.CFrame.RightVector
	end
	
	for _=1,10 do
		if not core.raycast(origin,dir*2) and core.raycast(origin,Vector3.new(0,-7,0)) then
			origin += dir*2
		else
			origin += dir*-2
			break
		end
	end
	
	local _,result = core.raycast(origin,(status:get("currentTarget").Position - origin).Unit * 30)
	if result then
		if result.Instance:IsDescendantOf(status:get("currentTarget").Parent) then

		else

			origin = myRoot.Position
		end
		origin = myRoot.Position
	end
	
	return origin
end

function checkDirection(dir)
	local goalPos = status:get("currentTarget").Position
	local origin = myRoot.Position
	
	local success = false
	for i=1, 5 do
		if not core.raycast(origin,dir*1) and core.raycast(origin,Vector3.new(0,-7,0)) then
			origin = origin + dir*1
		else
			origin = origin + dir*-1
			break
		end
		
		if i == 5 then
			success = true
		end
	end
	
	return success,origin
end

function movement.retreat()
	
	--Check if we can retreat directly from the enemy.
	local result,pos = checkDirection(-(status:get("currentTarget").Position - myRoot.Position).Unit)
	if result then
		return pos
	end
	
	--Check if we can move away to the right.
	result,pos = checkDirection(myRoot.CFrame.RightVector)
	if result then
		return pos
	end
	
	--Check if can move away to the left.
	result,pos = checkDirection(-myRoot.CFrame.RightVector)
	if result then
		return pos
	end
	
	return myRoot.Position
end

function movement.slowAdvance()
	local goalPos = status:get("currentTarget").Position
	local origin = myRoot.Position
	
	local maxIterations = 30
	repeat
		maxIterations -= 1
		if not core.raycast(origin,(goalPos - origin).Unit*1) and core.raycast(origin,Vector3.new(0,-7,0)) then
			origin = origin + (goalPos - origin).Unit*1
		else
			origin = origin + (goalPos - origin).Unit*-1
			break
		end
		if maxIterations <= 0 then
			break
		end
	until false 
	
	return origin
end