void AutoDraw() { //read data from data manager if (pastPainting == null) { pastPainting = dataManager.ReadDataFromPath(dataManager.readPath); if (pastPainting == null) { guiTimer = 15; SwtichMode(); return; } } //draw first point if (autoDrawData.nowIndex > pastPainting.positionData.Length - 1 || autoDrawData.nowIndex <= 0) { Vector3 point = pastPainting.positionData[0]; drawLine.CreateLine(point); autoDrawData.nowIndex = 1; autoDrawData.currentPos = pastPainting.positionData[0]; currentPosition = autoDrawData.currentPos; } //calculate currentPosition if (Vector3.Distance(pastPainting.positionData[autoDrawData.nowIndex], currentPosition) <= 0.1f) { autoDrawData.nowIndex++; } else { Vector3 lastPosition = autoDrawData.nowIndex == 0 ? pastPainting.positionData[autoDrawData.nowIndex] : pastPainting.positionData[autoDrawData.nowIndex - 1]; Vector3 nextPosition = pastPainting.positionData[autoDrawData.nowIndex]; Vector3 deltaMoveDirection = (nextPosition - lastPosition).normalized * speed * Time.deltaTime; currentPosition += deltaMoveDirection; //check the position won't go too far away float startToCurrent = Vector3.Distance(lastPosition, currentPosition); float startToEnd = Vector3.Distance(lastPosition, nextPosition); while (startToCurrent > startToEnd) { autoDrawData.currentPos = currentPosition; drawLine.UpdateLine(nextPosition); autoDrawData.nowIndex++; if (autoDrawData.nowIndex > pastPainting.positionData.Length - 1) { return; } lastPosition = pastPainting.positionData[autoDrawData.nowIndex - 1]; nextPosition = pastPainting.positionData[autoDrawData.nowIndex]; startToCurrent = Vector3.Distance(lastPosition, currentPosition); startToEnd = Vector3.Distance(lastPosition, nextPosition); currentPosition = (nextPosition - lastPosition).normalized * startToCurrent + lastPosition; } autoDrawData.currentPos = currentPosition; drawLine.UpdateLine(autoDrawData.currentPos); } }