Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ILoveMyDada · Jul 08, 2017 at 01:05 AM · collisionbooleanif-statementslocalscale

Changing localScale on Collision2D

I'm trying to have it so that when a ball hits a moving paddle, the paddle gets smaller each time it gets hit on collision.

Right now I have it so that when the bool RightYellowWallMovedBack is true, then it waits 6 seconds to change the x scale of the paddle to 10. THEN, once the ball collides with the paddle, it should change the x scale to 8, and so on and so forth. But it goes directly to the code below it, which is 6. It's skipping the first collision.

Any ideas?

  public IEnumerator paddleScaling()
 {
     if (Ball8Layer5GreenBall.GetComponent<Ball8Layer5GreenCollide> ().RightYellowWallMovedBack == true) 
     {
         yield return new WaitForSeconds (6);                                                

         paddleScale10IsON = true;
         this.transform.localScale = new Vector3 (10f, 0.3f, 1f);
         

     }
 }



 void OnCollisionEnter2D(Collision2D paddleScaleCollider)
 {
     if (paddleScaleCollider.gameObject.tag == "ball" && paddleScale10IsON == true)             
     {
         this.transform.localScale = new Vector3 (8f, 0.3f, 1f);
         paddleScale8IsON = true;
     }


     if (paddleScaleCollider.gameObject.tag == "ball" && paddleScale8IsON == true)             
     {
         paddleScale6IsON = true; 
         this.transform.localScale = new Vector3 (6f, 0.3f, 1f);
     }
Comment
Add comment
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

2 Replies

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

Answer by FlaSh-G · Jul 08, 2017 at 11:48 AM

You didn't post any code that would start the paddleScaling coroutine. So it's kinda hard to see what you're doing there.

Anyway... the names in your code are kinda scary. If you have class names or variable names that are the same except for a changing number part, you're doing something very wrong. Computers are great with numbers and math, so you should use that instead!

Let's start with your ifs that, I assume, go on for quite a bit after the code you posted. Instead of having lots of boolean variables, why not use a number that indicates the current size? If you look closely, that number already exists - it's transform.localScale.x. It is always 8 exactly when paddleScale8IsON is true. So you can simply do this:

 void OnCollisionEnter2D(Collision2D paddleScaleCollider)
 {
   if(paddleScaleCollider.gameObject.CompareTag("ball")) // Use CompareTag instead of ==
   {
     var paddleScale = transform.localScale.x;
     if(paddleScale > 2f)
     {
       paddleScale -= 2f;
     }
     transform.localScale = new Vector3(paddleScale, 0.3f, 1f);
   }
 }

and that would be all.

Now, I am not sure if I understood you correctly, but i think you want to do all this only as soon as the Coroutine went through once? in that case, you'd probably want to add a single bool variable:

 private bool isScaling = false;

which you set to true at the end of your Coroutine:

 yield return new WaitForSeconds (6);
 transform.localScale = new Vector3 (10f, 0.3f, 1f);
 isScaling = true;

and then you add that to the one if condition of your OnCollisionEnter2D:

 void OnCollisionEnter2D(Collision2D paddleScaleCollider)
 {
   if(isScaling && paddleScaleCollider.gameObject.CompareTag("ball"))
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 ILoveMyDada · Jul 08, 2017 at 01:04 PM 0
Share

Hey yeah sorry, I didn't really have a Coroutine start anywhere above anyway. Just a Start to declare certain variables.

But I tried this and it worked, thanks! Easier to use numbers than bools for sure. But Ima keep my scary names. I like em.

Cheers!

avatar image
0

Answer by Raimi · Jul 08, 2017 at 12:38 PM

You could use Mecanim to animate it. Then you can set perameters to the animation and trigger it through code using animator. SetTrigger, animator.SetBool etc

You could also add some cool effects using Mecanim that would be difficult just in code.

Comment
Add comment · 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

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

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

How do you trigger a collider to become enabled after the player collider isn't touching it anymore? 4 Answers

Collecting Array items in order 1 Answer

WaitForSeconds twice in 1 if statement, not working 2 Answers

Only able to jump when grounded.,Only doing something if something is true 1 Answer

Why is my collision function not calling my method inside of it? 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