void applyNegativeForce(NegativeBubbleBehaviour nbubble) { float parameter = 1.5f; var direction = gameObject.transform.position - nbubble.transform.position; nbubble.GetComponentInParent <Rigidbody2D>().AddForce(parameter * direction.normalized / direction.magnitude); }
// Update is called once per frame void Update() { //increment countdown countdown -= Time.deltaTime; Object[] bubbles = FindObjectsOfType(typeof(BubbleBehaviour)); Object[] negativeBubbles = FindObjectsOfType(typeof(NegativeBubbleBehaviour)); Object[] magneticBubbles = FindObjectsOfType(typeof(MagneticBubbleBehaviour)); //spawn bubbles if not enough bubbles on screen if (bubbles.Length < 5) { Spawn(); } Touch[] myTouches = Input.touches; for (int i = 0; i < Input.touchCount; i++) { Debug.Log("Input position: " + i + " " + Input.GetTouch(i).position); //2D solution Vector3 pos = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position); RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero); if (hit.collider != null) { Debug.Log("Hitting:" + hit.collider.name); BubbleBehaviour bubble = hit.collider.gameObject.GetComponent(typeof(BubbleBehaviour)) as BubbleBehaviour; if (bubble != null) { bubble.onPop(); } NegativeBubbleBehaviour negativeBubble = hit.collider.gameObject.GetComponent(typeof(NegativeBubbleBehaviour)) as NegativeBubbleBehaviour; if (negativeBubble != null) { negativeBubble.onPop(); } MagneticBubbleBehaviour magneticBubble = hit.collider.gameObject.GetComponent(typeof(MagneticBubbleBehaviour)) as MagneticBubbleBehaviour; if (magneticBubble != null) { magneticBubble.onPop(); } } } //fallback for testing on the PC if (Input.GetMouseButtonDown(0)) { Debug.Log("Mouse down"); Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition); RaycastHit2D hit = Physics2D.Raycast(pos, Vector2.zero); if (hit.collider != null) { Debug.Log("Hitting:" + hit.collider.name); BubbleBehaviour bubble = hit.collider.gameObject.GetComponent(typeof(BubbleBehaviour)) as BubbleBehaviour; if (bubble != null) { bubble.onPop(); } NegativeBubbleBehaviour negativeBubble = hit.collider.gameObject.GetComponent(typeof(NegativeBubbleBehaviour)) as NegativeBubbleBehaviour; if (negativeBubble != null) { negativeBubble.onPop(); } MagneticBubbleBehaviour magneticBubble = hit.collider.gameObject.GetComponent(typeof(MagneticBubbleBehaviour)) as MagneticBubbleBehaviour; if (magneticBubble != null) { magneticBubble.onPop(); } } } }