// Update is called once per frame private void Update() { if (_intervalTimer > 0) { _intervalTimer -= Time.deltaTime; return; } // Update the platform position if (_localTransform) { transform.localPosition = Vector3.Lerp(_positions[_currentPosition], _positions[_targetPosition], (_timer += Time.deltaTime) / _speedInSeconds); } else { transform.position = Vector3.Lerp(_positions[_currentPosition], _positions[_targetPosition], (_timer += Time.deltaTime) / _speedInSeconds); } // If we're approximately in the final position, change the indexes and restart the timer. if (Vector3Util.Approx((_localTransform ? transform.localPosition : transform.position), _positions[_targetPosition], 0.01f)) { switch (_loopMode) { case LoopModes.Circular: // Circular mode is simple, keep incrementing and user modulus to go back to start of the list when you hit the end. _currentPosition = (_currentPosition + 1) % _positions.Count; _targetPosition = (_targetPosition + 1) % _positions.Count; break; case LoopModes.Reverse: // Reverse mode needs to check if it's going (targetPosition > currentPosition) or coming back (currentPosition > targetPosition) // Then increment/decrement accordingly or change the direction if it hits the end/start of the list. if (_targetPosition > _currentPosition) { if (_targetPosition + 1 < _positions.Count) { _currentPosition++; _targetPosition++; } else { _currentPosition = _positions.Count - 1; _targetPosition = _positions.Count - 2; } } else { if (_targetPosition - 1 > -1) { _currentPosition--; _targetPosition--; } else { _currentPosition = 0; _targetPosition = 1; } } break; default: Debug.LogError("Invalid platform mode."); break; } _timer = 0; _intervalTimer = _movingInterval; } }