コード例 #1
0
    void Update()
    {
        //Movement
        if (isDodging && Vector2.Distance(rb.velocity, Vector2.zero) < distanceToRecover)
        {
            isDodging            = false;          //return isDodging boolean to false
            rb.drag              = defaultDrag;    //restore normal linear Drag
            boxCollider.size     = defaultBoxSize; // TODO: program proper dodging mechanics
            transform.localScale = Vector2.one;    // return transform scale to 1,1

            spriteRenderer.sprite = normalSprite;
        }
        else if (isDodging)
        {
            //transform.localScale = new Vector2(2, 2);
            spriteRenderer.sprite = dodgeSprite;
        }

        if (!isDodging)
        {
            rb.velocity = Vector2.zero;

            if (canMove)
            {
                if (Input.GetKey(KeyCode.A))
                {
                    rb.velocity += Vector2.left * movementSpeed;
                    xDirection   = xdir.left;
                    direction    = Vector2.left;
                }
                else if (Input.GetKey(KeyCode.D))
                {
                    rb.velocity += Vector2.right * movementSpeed;
                    xDirection   = xdir.right;
                    direction    = Vector2.right;
                }
                else
                {
                    yDirection = ydir.none;
                }

                if (Input.GetKey(KeyCode.W))
                {
                    rb.velocity += Vector2.up * movementSpeed;
                    yDirection   = ydir.up;
                    direction    = Vector2.up;
                }
                else if (Input.GetKey(KeyCode.S))
                {
                    rb.velocity += Vector2.down * movementSpeed;
                    yDirection   = ydir.down;
                    direction    = Vector2.down;
                }
                else
                {
                    yDirection = ydir.none;
                }

                if (Input.GetKeyDown(KeyCode.Space))
                {
                    rb.velocity    = new Vector2(Sign(rb.velocity.x), Sign(rb.velocity.y)) * dodgeForce;
                    rb.drag        = dodgeDrag;
                    defaultBoxSize = boxCollider.size;
                    //boxCollider.size = Vector2.zero;
                    isDodging = true;
                }
            }



            if (Input.GetKeyDown(KeyCode.J))
            {
                Attack();
            }
        }
    }
コード例 #2
0
    void Update()
    {
        //Movement
        if (isDodging && Vector2.Distance(rb.velocity, Vector2.zero) < distanceToRecover)
        {
            isDodging            = false;          //return isDodging boolean to false
            rb.drag              = defaultDrag;    //restore normal linear Drag
            boxCollider.size     = defaultBoxSize; // TODO: program proper dodging mechanics
            transform.localScale = Vector2.one;    // return transform scale to 1,1

            spriteRenderer.sprite = normalSprite;
        }
        else if (isDodging)
        {
            //transform.localScale = new Vector2(2, 2);
            spriteRenderer.sprite = dodgeSprite;
        }

        if (!isDodging)
        {
            rb.velocity = Vector2.zero;

            if (canMove)
            {
                if (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))
                {
                    rb.velocity += Vector2.left * movementSpeed;
                    xDirection   = xdir.left;
                    direction    = Vector2.left;
                }
                else if (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))
                {
                    rb.velocity += Vector2.right * movementSpeed;
                    xDirection   = xdir.right;
                    direction    = Vector2.right;
                }
                else
                {
                    yDirection = ydir.none;
                }

                if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
                {
                    rb.velocity += Vector2.up * movementSpeed;
                    yDirection   = ydir.up;
                    direction    = Vector2.up;
                }
                else if (Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.S))
                {
                    rb.velocity += Vector2.down * movementSpeed;
                    yDirection   = ydir.down;
                    direction    = Vector2.down;
                }
                else
                {
                    yDirection = ydir.none;
                }

                if (Input.GetKeyDown(KeyCode.Space))
                {
                    rb.velocity    = new Vector2(Sign(rb.velocity.x), Sign(rb.velocity.y)) * dodgeForce;
                    rb.drag        = dodgeDrag;
                    defaultBoxSize = boxCollider.size;
                    //boxCollider.size = Vector2.zero;
                    isDodging = true;
                }
            }

            if (Input.GetKeyDown(KeyCode.J) || Input.GetMouseButtonDown(0))
            {
                if (equippedWeapon == wpn.sword || equippedWeapon == wpn.spear)
                {
                    AttackSlash();
                }
                else if (equippedWeapon == wpn.bow && canShoot)
                {
                    AttackBow();
                }
            }

            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                equippedWeapon = wpn.sword;
                UpdateHUD();
            }
            else if (Input.GetKeyDown(KeyCode.Alpha2) || Input.GetMouseButtonDown(1))
            {
                equippedWeapon = wpn.bow;
                UpdateHUD();
            }
            else if (Input.GetKeyDown(KeyCode.Alpha3) || Input.GetMouseButtonDown(2))
            {
                equippedWeapon = wpn.spear;
                UpdateHUD();
            }
        }
    }