示例#1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.touchCount > 0)         // if there are touches
        {
            for (int i = 0; i < Input.touchCount; i++)
            {
                Touch   t        = Input.GetTouch(i);
                Vector2 touchPos = t.position;

                if (Vector2.Distance(touchPos, startMoveButtonPosition) < Vector2.Distance(touchPos, startShootButtonPosition))   // if touch is closer to move button
                {
                    if (Vector2.Distance(touchPos, startMoveButtonPosition) <= delta || moveButtonPressed)                        // if move button is pressed

                    {
                        moveButtonPressed = true;
                        p.move((touchPos - startMoveButtonPosition).normalized); // tells character to move
                        if (t.phase == TouchPhase.Ended)                         // if player pulled finger
                        {
                            moveButtonPressed = false;
                            p.stop();                              // tells character to stop
                        }
                    }
                }
                else
                {
                    if (Vector2.Distance(touchPos, startShootButtonPosition) <= delta || shootButtonPressed)
                    {
                        shootButtonPressed = true;
                        p.attack((touchPos - startShootButtonPosition).normalized); // tells character to attack
                        if (t.phase == TouchPhase.Ended)                            // if player pulled finger
                        {
                            p.triggerUp();
                            shootButtonPressed = false;
                        }
                    }
                }
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.W))
        {
            p.move(new Vector2(1, 1).normalized);
        }
        else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.S))
        {
            p.move(new Vector2(1, -1).normalized);
        }
        else if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.W))
        {
            p.move(new Vector2(-1, 1).normalized);
        }
        else if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.S))
        {
            p.move(new Vector2(-1, -1).normalized);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            p.move(Vector2.right);
        }
        else if (Input.GetKey(KeyCode.A))
        {
            p.move(-Vector2.right);
        }
        else if (Input.GetKey(KeyCode.W))
        {
            p.move(Vector2.up);
        }
        else if (Input.GetKey(KeyCode.S))
        {
            p.move(-Vector2.up);
        }
        else if (Input.GetKeyUp(KeyCode.S))
        {
            p.stopY();
        }
        else if (Input.GetKeyUp(KeyCode.A))
        {
            p.stopX();
        }
        else if (Input.GetKeyUp(KeyCode.D))
        {
            p.stopX();
        }
        else if (Input.GetKeyUp(KeyCode.W))
        {
            p.stopY();
        }

        if (Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.UpArrow))
        {
            p.attack(new Vector2(1, 1).normalized);
        }
        else if (Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.DownArrow))
        {
            p.attack(new Vector2(1, -1).normalized);
        }
        else if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.UpArrow))
        {
            p.attack(new Vector2(-1, 1).normalized);
        }
        else if (Input.GetKey(KeyCode.LeftArrow) && Input.GetKey(KeyCode.DownArrow))
        {
            p.attack(new Vector2(-1, -1).normalized);
        }
        else if (Input.GetKey(KeyCode.RightArrow))
        {
            p.attack(Vector2.right);
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            p.attack(-Vector2.right);
        }
        else if (Input.GetKey(KeyCode.UpArrow))
        {
            p.attack(Vector2.up);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            p.attack(-Vector2.up);
        }
        else if (Input.GetKeyUp(KeyCode.DownArrow))
        {
            p.triggerUp();
        }
        else if (Input.GetKeyUp(KeyCode.LeftArrow))
        {
            p.triggerUp();
        }
        else if (Input.GetKeyUp(KeyCode.RightArrow))
        {
            p.triggerUp();
        }
        else if (Input.GetKeyUp(KeyCode.UpArrow))
        {
            p.triggerUp();
        }
    }