On key press Z, Object A creates Object B and Object C right where it is, though Object C is invisible and has its’ collider disabled. As long as Z is pressed, Object B and C keep growing up in size. On key press up, Object B is reset back to it’s original size but, Object C becomes visible and the collider active, and whatever enemy it can touch for a brief second get killed and its’ size gets reset back to its’ original size and is ready to wait for the next key press to replicate the same behavior. All of this is working just fine for me.
Here is a scenario - If an Enemy touches Object B while the key is pressed, the Object A is damaged and loses some health. Since, collisions are detected each frame, in order for all the health to not reduce to zero, Object A is made invulnerable for 2 seconds, and then is made vulnerable back again for further damage. This behavior is working fine as well.
But, when I have the key pressed, and Enemy touched Object B and made Object A invulnerable for 2 seconds, suppose I did not stop pressing the key, the Object B becomes disabled but the Object C continues to grow, though invisible and when I let go of the key, it shows a really big Object C, and that is not what I wish for it to happen. So basically, whenever there is a collision DURING a key press, Object C should be reset to its’ original scale and wait for the next time the key is pressed.
Code for creating and scaling Object C
float speed = 0.2f;
float colliderspeed = 0.015f;
public GameObject blastPrefab;
private CircleCollider2D colliderBlast;
public GameObject Player;
void Start(){
blastPrefab.renderer.enabled = false;
blastPrefab.collider2D.enabled = false;
}
void Update (){
if(Input.GetKeyDown(KeyCode.Z))
{
blastPrefab.SetActive(true); //Instantiating the prefab
blastPrefab.renderer.enabled = false;
blastPrefab.collider2D.enabled = false;
blastPrefab.transform.parent = transform;
blastPrefab.transform.localPosition = new Vector3(0,0,0); //Follow Player
colliderBlast = blastPrefab.GetComponent<CircleCollider2D>();
}
if (Input.GetKey(KeyCode.Z))
{
blastPrefab.renderer.enabled = false;
blastPrefab.collider2D.enabled = false;
if (blastPrefab != null)
{
colliderBlast = blastPrefab.GetComponent<CircleCollider2D>();
colliderBlast.radius += Time.deltaTime * colliderspeed ;
blastPrefab.transform.localScale += gameObject.transform.localScale * (Time.deltaTime * speed ); //Scale the object
}
}
if(Input.GetKeyUp(KeyCode.Z))
{
if (!Player.GetComponent<PlayerDamage>().invulnerable){
blastPrefab.renderer.enabled = true;
blastPrefab.collider2D.enabled = true;
//blastPrefab.SetActive(true);
StartCoroutine(FlashBlast());
}
}
}
IEnumerator FlashBlast() //Blink every time.
{
yield return new WaitForSeconds(0.3f);
blastPrefab.renderer.enabled = true;
blastPrefab.collider2D.enabled = true;
yield return new WaitForSeconds(0.08f);
blastPrefab.renderer.enabled = false;
blastPrefab.collider2D.enabled = false;
blastPrefab.transform.localScale = new Vector3(0.1f,0.1f,0.1f); // make sure to reset the scale!
colliderBlast.radius = 3.14f;
}
Code for checking collisions on Object C
void OnTriggerEnter2D(Collider2D blastCollisionCheck)
{
if (blastCollisionCheck.gameObject.tag == "Crowd") {
//gameObject.transform.localScale = new Vector3(0.1f,0.1f,0.1f); // reset the scale!
//gameObject.GetComponent<CircleCollider2D>().radius = 3.14f;
//gameObject.SetActive(false);
score++;
UpdateScore();
Destroy (blastCollisionCheck.gameObject);
blastIsOn = false;
}
}