Example #1
0
    private void Update()
    {
        // Orient to face movement direction
        if (_rb.velocity.sqrMagnitude > 0.01f && !IsStunned)
        {
            Quaternion desiredRot = Quaternion.LookRotation(_rb.velocity, Vector3.up);
            transform.rotation = Mathfx.Damp(transform.rotation, desiredRot, 0.25f, Time.deltaTime * 5);
        }

        // Roll based on movement
        float targetZRot = Mathf.Abs(_moveVector.x) > 0.1f ? Mathf.Sign(_moveVector.x) * -90 : 0;

        if (IsStunned || _roomInhabitant.IsBeingSuckedIntoSpace)
        {
            targetZRot = 0;
        }

        _zRot = Mathfx.Damp(_zRot, targetZRot, 0.5f, Time.deltaTime * 5);
        _visualRoot.localEulerAngles = _visualRoot.localEulerAngles.WithZ(_zRot);

        // Update idle anim state
        if (_roomInhabitant.IsBeingSuckedIntoSpace)
        {
            _currentIdleState = AstronautIdle.Panic;
        }
        else if (IsStunned)
        {
            _currentIdleState = AstronautIdle.Stunned;
        }
        else if (_moveVector.sqrMagnitude > 0.01f)
        {
            _currentIdleState = AstronautIdle.Move;
        }
        else
        {
            _currentIdleState = AstronautIdle.Idle;
        }

        // Blend idle state
        _idleBlend = Mathfx.Damp(_idleBlend, (float)_currentIdleState, 0.25f, Time.deltaTime * 5);
        _animator.SetFloat(kAnimIdleState, _idleBlend);

        // Handle sucked into space
        if (_roomInhabitant.IsBeingSuckedIntoSpace)
        {
            if (_hideOnDie[0].activeSelf)
            {
                foreach (GameObject obj in _hideOnDie)
                {
                    obj.SetActive(false);
                }

                foreach (GameObject obj in _showOnDie)
                {
                    obj.SetActive(true);
                }
            }

            _deathTimer += Time.deltaTime;
            if (!_isDead && (_roomInhabitant.Room == null || _deathTimer > 5))
            {
                _rb.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
                _rb.AddTorque(Random.onUnitSphere * 1, ForceMode.VelocityChange);
                _isDead = true;

                if (_deathSound)
                {
                    AudioManager.Instance.PlaySound(gameObject, _deathSound);
                }

                Died?.Invoke();
            }

            if (_isDead && (!Mathfx.IsPointInViewport(transform.position, Camera.main) || _deathTimer > 10))
            {
                StartCoroutine(DieAsync());
                return;
            }
        }

        if (_stunFx != null)
        {
            _stunFx.SetActive(IsStunned);
        }

        if (!_roomInhabitant.IsBeingSuckedIntoSpace)
        {
            _attackCooldownTimer -= Time.deltaTime;
        }

        if (_exclamationEffect != null)
        {
            _exclamationEffect.SetActive(_attackCooldownTimer > 0);
        }

        _stunTimer -= Time.deltaTime;
    }