Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by coolbird22 · Apr 29, 2014 at 05:56 PM · collisiongameobjectscalereset

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;
 }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image coolbird22 · Apr 30, 2014 at 04:23 PM 0
Share

Anyone ? ... Someone? Please help !

avatar image LMan · Apr 30, 2014 at 07:56 PM 0
Share

Well, if you added a check for if the key was down in TriggerEnter, that would reset the scale yes?

avatar image ThePunisher · Apr 30, 2014 at 08:35 PM 0
Share

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.

avatar image ThePunisher · Apr 30, 2014 at 08:38 PM 0
Share

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
 }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ThePunisher · May 01, 2014 at 05:07 PM 0
Share

Np, glad you figured out.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Get Object that Instantiated this object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges