private void processCharge() { if (Input.GetButtonDown("Fire2")) { if (hasCharge == false) { hasCharge = true; Vector3 mouse3d = Camera.main.ScreenToWorldPoint(Input.mousePosition); Vector2 mouse = mouse3d; float angle = Mathf.Atan2(mouse.y - transform.position.y, mouse.x - transform.position.x); float xVel = Mathf.Cos(angle) * bombVel; float yVel = Mathf.Sin(angle) * bombVel; bomb = Instantiate(bombPrefab, transform.position, Quaternion.identity); Rigidbody2D rbBomb = bomb.GetComponent <Rigidbody2D>(); rbBomb.velocity = new Vector2(xVel, yVel); rbBomb.angularVelocity = UnityEngine.Random.Range(50f, 300f); } else { hasCharge = false; BombBehavior bombBehavior = bomb.GetComponent <BombBehavior>(); bombBehavior.Explode(); } } }
//Everything that occurs when a Collider2D by the name of other enters the collision box private void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Apple") //if the other collider's tag is called "Apple"... { combo += 1; //combo is incremented by 1 score += 10 * combo; //score is updated by 10 * (the currect combo) aTimer = 1.0f; //aTimer is reset to 1.0f sounds[0].Play(); //the apple pick sound is played Destroy(other.gameObject); //the apple is destroyed } else if ((other.tag == "Bomb") && (iTimer <= 0.0f)) //if the other collider's tag is called "Bomb" and iTimer is less than or equal to 0... { health -= 1; //health is decremented by 1 combo = 0; //combo is set to 0 BombBehavior otherBomb = other.GetComponent <BombBehavior>(); //a variable otherBomb of dataType BombBehavior (see BombBehavior script) is set to the attached component BombBehavior of the entering collider sounds[1].Play(); //the bomb sound is played otherBomb.Explode(); //otherBomb's Explode method is initialized iTimer = 3.0f; //iframe Timer is set to 3 seconds } else if (other.tag == "Feather") //if the other collider's tag is called "Feather"... { health += 1; //increment health by 1 StartCoroutine(LifeUpAnimation()); //start LifeUP animation sounds[2].Play(); //the LifeUP sound is played Destroy(other.gameObject); //destroy the feather } }