Exemple #1
0
    protected void Update()
    {
        if (!_stopMoving && _health.IsAlive)
        {
            if (_currentPath.ReachedEnd(_currentWaypointIndex))
            {
                _stopMoving = true;
                return;
            }

            _progress = Mathf.Clamp(_progress + Time.deltaTime * _movementSpeed, 0f, _timeToProgress);
            Vector3 waypointPosStep = _currentPath.GetPosition(_currentWaypointIndex, _progress / _timeToProgress);

            float xDir = -Mathf.Sign(waypointPosStep.x - transform.position.x);

            Vector3 scale = transform.localScale;
            scale.x = Mathf.Abs(scale.x) * xDir;
            transform.localScale = scale;

            transform.position = waypointPosStep;

            if (Mathf.Approximately(_progress, _timeToProgress))
            {
                _progress = 0f;
                _currentWaypointIndex++;
                _timeToProgress = Vector3.Distance(transform.position, _currentPath.GetPosition(_currentWaypointIndex, 1f)) / _movementSpeed;
            }
        }
    }