My attempt at getting the slime to move around

This is probably not great code… one commented out line and unity will lock up sooo… yeah

    private IEnumerator RoamingRoutine()
    {
        while (_state == State.Roaming)
        {
            Vector2 roamPosition = GetRoamingPosition();
            
            Vector2 currentPosition = _rigidbody2D.position;
            Vector2 newPosition = currentPosition + roamPosition;
            
            while (_elapsedTime < _roamingMoveDuration)
            {
                _elapsedTime += Time.deltaTime;
                float percentageComplete = _elapsedTime / _roamingMoveDuration;
                _rigidbody2D.MovePosition(Vector2.Lerp(currentPosition, newPosition, percentageComplete));
                yield return new WaitForSeconds(Time.deltaTime);
            }

            _elapsedTime = 0;
            yield return Helpers.GetWait(2f);
        }
    }

Privacy & Terms