コード例 #1
0
    void handleMouseDrag()
    {
        if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
        {
            Time.timeScale = .15f;
        }
        else
        {
            Time.timeScale = 1;
        }

        //Store the position of the mouse when initiating a mouse drag
        if (Input.GetMouseButtonDown(0))
        {
            mouseHolder0 = Input.mousePosition;
        }
        if (Input.GetMouseButtonDown(1))
        {
            mouseHolder1 = Input.mousePosition;
            weapon.ready();
        }

        //Retrieve the position of the mouse while dragging, draw a ray showing the drag
        if (Input.GetMouseButton(0))
        {
            mouseDrag0 = Vector2.ClampMagnitude(((Vector2)Input.mousePosition - mouseHolder0) / -5, maxMagnitude);
            Debug.DrawRay(transform.position, mouseDrag0, Color.green);
        }
        if (Input.GetMouseButton(1))
        {
            mouseDrag1 = Vector2.ClampMagnitude(((Vector2)Input.mousePosition - mouseHolder1) / -5, maxMagnitude);
            Debug.DrawRay((Vector2)transform.position, mouseDrag1, Color.red);
            Debug.DrawRay((Vector2)transform.position, mouseDrag1 + rb.velocity / 5, Color.magenta);
        }

        //On mouse release, move the player or fire projectiles
        if (Input.GetMouseButtonUp(0))
        {
            float speed = mouseDrag0.magnitude / maxMagnitude * (maxSpeed - minSpeed) + minSpeed;
            rb.velocity = mouseDrag0.normalized * speed;
            mouseDrag0  = Vector2.zero;
        }
        if (Input.GetMouseButtonUp(1))
        {
            //Debug.Log(mouseDrag1 + "\n" + mouseDrag1.magnitude / maxMagnitude);
            weapon.fire();
            mouseDrag1 = Vector2.zero;
        }
    }