Пример #1
0
    // the Update loop contains a very simple example of moving the character around and controlling the animation
    void Update()
    {
        if (gameObject.transform.position.y <= -100 && !_dead)
        {
            _dead   = true;
            _diedAt = Time.realtimeSinceStartup;
        }

        if (_dead && Time.realtimeSinceStartup > _diedAt + 2.0f)
        {
            _player.IncrementScore();
            GameObject.Destroy(gameObject);
            return;
        }

        if (!_controller.enabled)
        {
            return;
        }

        if (_controller.isGrounded)
        {
            _velocity.y = 0;
        }

        if (_ia.GetKey(KeyCode.Keypad6))
        {
            normalizedHorizontalSpeed = 1;
            if (transform.localScale.x < 0f)
            {
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
            }

            if (_controller.isGrounded)
            {
                _animator.SetBool("run", true);
            }
        }
        else if (_ia.GetKey(KeyCode.Keypad4))
        {
            normalizedHorizontalSpeed = -1;
            if (transform.localScale.x > 0f)
            {
                transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
            }

            if (_controller.isGrounded)
            {
                _animator.SetBool("run", true);
            }
        }
        else
        {
            normalizedHorizontalSpeed = 0;

            if (_controller.isGrounded)
            {
                _animator.SetBool("run", false);
            }
        }



        // we can only jump whilst grounded
        if (_controller.isGrounded && _ia.GetKey(KeyCode.Keypad8))
        {
            _velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity);
            _animator.SetBool("run", false);
        }

        if (_ia.GetKey(KeyCode.Keypad0))
        {
            _animator.SetBool("attack", true);
            _aSource.clip = punch;
            _aSource.Play();
            isAttacking = true;
        }

        if (touched && health > 0)
        {
            _velocity.y          = Mathf.Sqrt(2f * jumpHeight * -gravity);
            _velocity.x          = 100 * -transform.localScale.x;
            transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);

            touched = false;
        }

        // apply horizontal speed smoothing it. dont really do this with Lerp. Use SmoothDamp or something that provides more control
        var smoothedMovementFactor = _controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?

        if (Time.realtimeSinceStartup > _nextHit)
        {
            _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor);
        }

        // apply gravity before moving
        _velocity.y += gravity * Time.deltaTime;

        _controller.move(_velocity * Time.deltaTime);

        // grab our current _velocity to use as a base for all calculations
        _velocity = _controller.velocity;
    }