Example #1
0
    void FixedUpdate()
    {
        if (!isDown)
        {
            //接地判定を得る
            isGround = ground.IsGround();

            //各種座標軸の速度を求める
            float xSpeed = GetXSpeed();
            float ySpeed = GetYSpeed();

            //アニメーションを適用
            SetAnimation();

            //移動速度を設定
            rb.velocity = new Vector2(xSpeed, ySpeed);
        }
        else
        {
            rb.velocity = new Vector2(0, -gravity);
        }
    }
    // Update is called once per frame
    void FixedUpdate()
    {
        float horizon  = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        seconds += Time.deltaTime;
        groundcheck groundscript = ground.GetComponent <groundcheck>();
        enemycheck  enemyscript  = enemycheck.GetComponent <enemycheck>();
        enemycheck  enemyscript2 = enemycheck2.GetComponent <enemycheck>();

        isGround = groundscript.IsGround();

        isEnemy  = enemyscript.IsEnemy();
        isEnemy2 = enemyscript2.IsEnemy();
        Rigidbody2D rb = GetComponent <Rigidbody2D>();

        //Rigidbody2D rb_enemy =
        if (isGround)
        {
            if (vertical > 0 && !isJump)
            {
                rb.velocity = new Vector2(0, 15f); //ここでジャンプ調整
                //animator_setting("run", false);
                audioSources[0].Play();
                isJump = true;
                if (transform.position.y >= 10)
                {
                    jump_ground = true;
                }
                else
                {
                    jump_ground = false;
                }
            }
            else
            {
                isJump = false;
            }
        }


        if (Input.GetKeyDown(KeyCode.Z))
        {
            enemycheck.SetActive(true);
            enemycheck2.SetActive(true);
            audioSources[1].Play();
            animator_setting("naguri", true);
        }
        else if (Input.GetKeyUp(KeyCode.Z))
        {
            animator_setting("naguri", false);
            enemycheck.SetActive(false);
            enemycheck2.SetActive(false);
        }
        if (Input.GetKey(KeyCode.X))
        {
            if (raputoru_camera != null)
            {
                raputoru_camera.SetActive(true);
            }
        }
        if (Input.GetKey(KeyCode.X) == false)
        {
            if (raputoru_camera != null)
            {
                raputoru_camera.SetActive(false);
            }
        }
        if (horizon > 0)
        {
            direction = 1.5f;
            animator_setting("run", true);
            sprite.flipX = false;
        }
        else if (horizon < 0)
        {
            direction = -1.5f;
            animator_setting("run", true);


            sprite.flipX = true;
        }
        else
        {
            direction = 0f;
            animator_setting("run", false);

            // animator_setting("jump", false);
        }
        rb.velocity = new Vector2(scroll * direction, rb.velocity.y);
        animator_setting("ground", isGround);
        animator_setting("jump", isJump);
        animator_setting("jump-ground", jump_ground);
    }
Example #3
0
    /// <summary>
    /// Y成分で必要な計算をし、速度を返す。
    /// </summary>
    /// <returns>Y軸の速さ</returns>
    private float GetYSpeed()
    {
        float verticalKey = Input.GetAxisRaw("Vertical");
        float ySpeed      = -gravity;

        if (isGround)
        {
            if (verticalKey > 0 && jumpTime < jumpLimitTime)
            {
                ySpeed   = jumpSpeed;
                jumpPos  = transform.position.y; //ジャンプした位置を記録する
                isJump   = true;
                jumpTime = 0.0f;
            }
            else
            {
                isJump = false;
            }
        }
        else if (head.IsGround())//!head.IsGroundを使うとバグる?理由は不明
        {
            isJump      = false;
            isOtherJump = false;
            jumpTime    = 0.0f;
        }
        //何かを踏んだ際のジャンプ
        else if (isOtherJump)
        {
            if (jumpPos + otherJumpHeight > transform.position.y && jumpTime < jumpLimitTime)
            {
                ySpeed    = jumpSpeed;
                jumpTime += Time.deltaTime;
            }
            else
            {
                isOtherJump = false;
                jumpTime    = 0.0f;
            }
        }
        //ジャンプ中
        else if (isJump)
        {
            //上ボタンを押されている。かつ、現在の高さがジャンプした位置から自分の決めた位置より下ならジャンプを継続する
            if (verticalKey > 0 && jumpPos + jumpHeight > transform.position.y && jumpTime < jumpLimitTime)
            {
                ySpeed    = jumpSpeed;
                jumpTime += Time.deltaTime;
            }
            else
            {
                isJump   = false;
                jumpTime = 0.0f;
            }
        }
        if (isJump)
        {
            ySpeed *= jumpCurve.Evaluate(jumpTime);
        }
        else
        {
            ySpeed *= gravityCurve.Evaluate(Time.deltaTime);
        }

        return(ySpeed);
    }