// Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { float distance = Vector3.Distance(transform.position, Input.mousePosition); if (distance <= joystick.BaseRadius) { gettingInput = true; } else if (Vector3.Distance(fireButtonTransform.position, Input.mousePosition) <= fireButton.ButtonRadius) { firing = true; } } if (Input.GetMouseButtonUp(0)) { gettingInput = false; firing = false; joystick.ResetHandle(); joystick.FireReleased(); } if (gettingInput) { joystick.ReceiveInput(Input.mousePosition); } if (firing) { joystick.FirePressed(); } }
void Update() { if (Input.touchCount > 0) { // handle new or lifted touches foreach (Touch touch in Input.touches) { // new touch if (touch.phase == TouchPhase.Began) { // if not already found the joystick finger if (!joystickPressed) { float distance = Vector3.Distance(joystickTransform.position, touch.position); // if touching joystick, this is the finger to look at if (distance <= joystick.BaseRadius) { joystickFingerId = touch.fingerId; joystickPressed = true; continue; // this finger is reserved - go to next } } // if not already presssing fire if (!firePressed) { float distance = Vector3.Distance(fireButtonTransform.position, touch.position); // if touching joystick, this is the finger to look at if (distance <= fireButton.ButtonRadius) { fireFingerId = touch.fingerId; firePressed = true; continue; // this finger is reserved - go to next } } } if (touch.phase == TouchPhase.Moved) { if (firePressed && touch.fingerId == fireFingerId) { float distance = Vector3.Distance(fireButtonTransform.position, touch.position); // finger moved off of fire button if (distance > fireButton.ButtonRadius) { firePressed = false; } } } if (touch.phase == TouchPhase.Ended) { // if finger was lifted off of joystick if (touch.fingerId == joystickFingerId) { joystickPressed = false; joystick.ResetHandle(); } if (touch.fingerId == fireFingerId) { firePressed = false; joystick.FireReleased(); } } } // handle movement of joystick finger if (joystickPressed) { Vector2 position = Array.Find(Input.touches, touch => touch.fingerId == joystickFingerId).position; joystick.ReceiveInput(position); } if (firePressed) { joystick.FirePressed(); } } }