- Home /
Size of object not resetting when Player becomes invulnerable. What am I missing here ? (Big Post)
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;
}
}
Well, if you added a check for if the key was down in TriggerEnter, that would reset the scale yes?
I'm looking at your script right now trying to figure out where the logic is broken but so far to me it just seems like you missed an edge case. Also, you should really cache your component references, especially if you are referencing them every frame like the CircleCollider2D and the PlayerDamage components.
I might just be criticizing your code here but what is the point of the null check for blastPrefab if you are accessing members of that object right before the check:
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.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
}
}
Answer by coolbird22 · May 01, 2014 at 06:43 AM
I just managed to figure this out. All I had to do was assign the Object C as a variable on Object B and make it inactive, on Object Bs' collision with Object A. So, Object C remains inactive until the Key is pressed again and makes it active. @ThePunisher - Thanks for calling me out on my code ethics. I will look into it and cache the component references so as to improve computation time. I had put the null check as it was the first thing I had added to the code, and now it doesn't naturally make sense to continue doing so.
Your answer
Follow this Question
Related Questions
Trouble Descaling 2D object 1 Answer
How to reset a scene from collision with a GameObject? 0 Answers
How to reset GameObject to original scale 2 Answers
Detect cloned object detection once 0 Answers