コード例 #1
0
    // the Update loop contains a very simple example of moving the character around and controlling the animation
    void Update()
    {
        //Choose between Alien and Spaceship
        if (inputMap.ChangeToAlien())
        {
            canMove = true;
        }
        else if (inputMap.ChangeToShip())
        {
            canMove = false;
        }

        //Reset position if falling off world
        if (this.transform.position.y < -7)
        {
            this.transform.position = new Vector3(0, 10, 0);
        }

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

            if (inputMap.GoRight())
            {
                normalizedHorizontalSpeed = 1;
                if (transform.localScale.x < 0f)
                {
                    transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
                }

                if (_controller.isGrounded && !isAttacking)
                {
                    _animator.Play(Animator.StringToHash("Run"));
                }
            }
            else if (inputMap.GoLeft())
            {
                normalizedHorizontalSpeed = -1;
                if (transform.localScale.x > 0f)
                {
                    transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
                }

                if (_controller.isGrounded && !isAttacking)
                {
                    _animator.Play(Animator.StringToHash("Run"));
                }
            }
            else if (inputMap.GoHorizontalAnalog() != 0f)
            {
                normalizedHorizontalSpeed = inputMap.GoHorizontalAnalog();

                transform.localScale = new Vector3(Mathf.Sign(normalizedHorizontalSpeed), transform.localScale.y, transform.localScale.z);

                if (_controller.isGrounded && !isAttacking)
                {
                    _animator.Play(Animator.StringToHash("Run"));
                }
            }
            else
            {
                normalizedHorizontalSpeed = 0;

                if (_controller.isGrounded && !isAttacking)
                {
                    _animator.Play(Animator.StringToHash("Idle"));
                }
            }

            if (inputMap.AttackNow() && !isAttacking)
            {
                isAttacking = true;
                StartCoroutine(WaitAttack());
                _animator.Play(Animator.StringToHash("Attack"));
            }

            // we can only jump whilst grounded
            if ((_controller.isGrounded || isClimbing) && inputMap.JumpNow())
            {
                _velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity);
                _animator.Play(Animator.StringToHash("Jump"));
            }

            //Climbing stairs (improvised code just to display the 'moving up' control input. Don't use it for your project)
            if (canClimb && inputMap.GoUp())
            {
                isClimbing = true;
            }

            if (isClimbing)
            {
                _velocity.y = 0f;;
            }

            if (isClimbing && inputMap.GoUp())
            {
                _animator.Play(Animator.StringToHash("Climb"));
                _velocity.y = 3f;
            }

            if (isClimbing && inputMap.GoDown())
            {
                _animator.Play(Animator.StringToHash("Climb"));
                _velocity.y = -3f;
            }

            if (inputMap.JumpNow())
            {
                isClimbing = false;
            }  //End of climbing hack code

            // 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?
            _velocity.x = Mathf.Lerp(_velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor);

            // apply gravity before moving (only if not climbing
            if (!isClimbing)
            {
                _velocity.y += gravity * Time.deltaTime;
            }

            // if holding down bump up our movement amount and turn off one way platform detection for a frame.
            // this lets uf jump down through one way platforms
            if (_controller.isGrounded && inputMap.GoDown())
            {
                _velocity.y *= 3f;
                _controller.ignoreOneWayPlatformsThisFrame = true;
            }

            _controller.move(_velocity * Time.deltaTime);

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