コード例 #1
0
    void Controls()
    {
        // Move left or right
        h = Input.GetAxis(axis);
        if (!isHitted)
        {
            rb.velocity = new Vector2(h * Time.deltaTime * speed, rb.velocity.y);
        }

        // Jump
        if (Input.GetButtonDown(jumpKey))
        {
            if (isGrounded)
            {
                isGrounded  = false;
                rb.velocity = new Vector2(rb.velocity.x, Time.deltaTime * jumpHeight);
            }
        }

        // Punch
        if (Input.GetButtonDown(punchKey) && (Time.time - lastPunch > .5 || lastPunch == 0))
        {
            lastPunch     = Time.time;
            lastPunchAnim = Time.time;
            idle.SetActive(false);
            punchScript.Punch();
        }

        // Special
        if (Input.GetButtonDown(specialKey) && (Time.time - lastSpecial > 5 || lastSpecial == 0))
        {
            lastSpecial = Time.time;

            audioSource.PlayOneShot(specialSound);

            float      specialX = transform.InverseTransformDirection(Vector3.left).x;
            GameObject hadouken = Instantiate(Resources.Load("Prefabs/Specials/Hadouken"), new Vector3(transform.position.x + specialX * 2, transform.position.y + 18f, -1), transform.rotation) as GameObject;
            hadouken.GetComponent <SpecialScript> ().player = transform.gameObject;
        }

        // Guard
        if (transform.InverseTransformDirection(Vector3.left).x *h > 0)
        {
            isGuarding = true;
        }
        else
        {
            isGuarding = false;
        }

        // Reset after punch
        if (Time.time - lastPunchAnim > 0.2f)
        {
            lastPunchAnim = 0;
            idle.SetActive(true);
            punchScript.StopPunch();
        }

        // Reset after hitted
        if (Time.time - lastHit > 0.5f)
        {
            lastHit  = 0;
            isHitted = false;
        }
    }