示例#1
0
    // Update is called once per frame
    void Update()
    {
        // For debug
        if (Input.GetKeyDown("left"))
        {
            MoveToLeft();
        }
        if (Input.GetKeyDown("right"))
        {
            MoveToRight();
        }
        if (Input.GetKeyDown("space"))
        {
            Jump();
        }

        if (IsStunned())
        {
            // When sleep state, proceed recovery count
            moveDirection.x = 0.0f;
            moveDirection.z = 0.0f;
            recoverTime    -= Time.deltaTime;
        }
        else
        {
            if (RunningParticles.isStopped && controller.isGrounded)
            {
                RunningParticles.Play();
            }
            speedZ += velocityZ * Time.deltaTime;
            // Slowly accelerate toward Z, used after being stunned
            float acceleratedZ = moveDirection.z + (stunAccelerationZ * Time.deltaTime);
            moveDirection.z = Mathf.Min(acceleratedZ, speedZ);

            // Calculate X direction movement
            float ratioX = (targetLane * LaneWidth - transform.position.x) / LaneWidth;
            moveDirection.x = ratioX * speedX;
        }

        // Adds as much force as gravity to every frame
        moveDirection.y -= gravity * Time.deltaTime;

        // Move Run
        Vector3 globalDirection = transform.TransformDirection(moveDirection);

        controller.Move(globalDirection * Time.deltaTime);

        // If the motor is grounded after movement, the speed in the Y direction is reset
        if (controller.isGrounded)
        {
            if (RunningParticles.isStopped)
            {
                RunningParticles.Play();
            }
            moveDirection.y = 0;
            hasAirJumped    = false;
        }

        if (transform.position.y < deathYBounds)
        {
            life--;
            if (life > 0)
            {
                recoverTime = StunDuration * 2;
                GameObject oldestStage = stageGenerator.GetStage(1);
                Vector3    pos         = oldestStage.transform.position;
                pos.y = 3;
                transform.position = pos;
                targetLane         = 0;
            }
        }

        // If the speed is more than 0, the running flag is set to true.
        animator.SetBool("run", moveDirection.z > 0.0f);
    }