Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 bogdankrstic · Mar 15, 2021 at 10:19 AM · if-statementscoroutinesienumeratorlocalscalewhile-loop

How to resize a player every couple of seconds?

Hi!

I'm trying to create a function that will decrease the size of my object every couple of seconds while the size is bigger than the x. When the size is smaller than the x, I don't want it to decrease anymore.

I've run the code and there are no errors showing up in the console but my game breaks every time.

Here's my code. I am a beginner so if you can suggest any off-topic changes that could help, I'd be very happy to hear:

 //Public fields
 public float Increase;
 public float Decrease;
 public float speed;

 public Vector3 left;
 public Vector3 right;
 public Vector3 forward;
 public Vector3 back;

 //Private fields
 private Rigidbody playerRb;

 // Start is called before the first frame update
 void Start()
 {
     playerRb = GetComponent<Rigidbody>();
     left = GameObject.Find("PlaneLeft").transform.position;
     right = GameObject.Find("PlaneRight").transform.position;
     forward = GameObject.Find("PlaneForward").transform.position;
     back = GameObject.Find("PlaneBack").transform.position;
 }

 // Update is called once per frame
 void Update()
 {
     MovePlayer();
     KeepPlayerInBounds();
     StartCoroutine(DecreaseSize());
 }
 
 //Decrease size every couple of seconds while the size is bigger than the x
 IEnumerator DecreaseSize()
 {
     while (transform.localScale.x > 5)
     {
         yield return new WaitForSeconds(5);
         transform.localScale -= new Vector3(Decrease, Decrease, Decrease);
     }
 }

 //Get bigger once player collides with food
 void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Food")
     {
         transform.localScale += new Vector3(Increase, Increase, Increase);
         Destroy(other.gameObject);
     }
 }

 //Player control movement
 void MovePlayer()
 {
     float horizontalInput = Input.GetAxis("Horizontal");
     float verticalInput = Input.GetAxis("Vertical");

     transform.Translate(Vector3.forward * verticalInput * speed * Time.deltaTime / (transform.localScale.x / 4));
     transform.Translate(Vector3.right * horizontalInput * speed * Time.deltaTime / (transform.localScale.x / 4));
 }

 //Keep player in the arena
 void KeepPlayerInBounds()
 {
     float BallBound = transform.localScale.x / 2;

     float leftBound = left.x + BallBound;

     if (transform.position.x < leftBound)
     {
         transform.position = new Vector3(leftBound, transform.position.y, transform.position.z);
     }
     
     float rightBound = right.x - BallBound;

     if (transform.position.x > rightBound)
     {
         transform.position = new Vector3(rightBound, transform.position.y, transform.position.z);
     }
    
     float forwardBound = forward.z - BallBound;

     if (transform.position.z > forwardBound)
     {
         transform.position = new Vector3(transform.position.x, transform.position.y, forwardBound);
     }

     float backBound = back.z + BallBound;

     if (transform.position.z < backBound)
     {
         transform.position = new Vector3(transform.position.x, transform.position.y, backBound);
     }




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
1
Best Answer

Answer by Smurfj3 · Mar 15, 2021 at 10:42 AM

Alright well you are currently running the decrease size coroutine every single frame since you are calling it in the Update method. So for instance if you want to run the decrease size method every 5 seconds, you can try something like this:

  float timer = 5;
 
     void Update()
     {
         timer -= Time.deltaTime * 1;
         if(timer < 0)
         {
             //5 seconds are up now lets run the decrease size method and reset the timer    
             timer = 5;
             StartCoroutine(DecreaseSize());
         }
     }

Secondly, you do this to decrease the size:

transform.localScale -= new Vector3(Decrease, Decrease, Decrease);

But I may be blind but I don't see you initializing Decrease variable anywhere unless you set it in the Inspector. So make sure it has a value. Also make use of Debug.Log("this is a test"); to see if all lines of code are reached. So for example try:

  IEnumerator DecreaseSize()
  {
      while (transform.localScale.x > 5)
      {
          yield return new WaitForSeconds(5);
          transform.localScale -= new Vector3(Decrease, Decrease, Decrease);
          Debug.Log("does it decrease or what");
      }
  }

This should get you started.

Comment
Add comment · Show 3 · 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 bogdankrstic · Mar 18, 2021 at 12:40 AM 0
Share

Works! But the thing is, the Decrease float keeps increasing every time an action is completed. I set it to 0.25 and then after the first decrease, it sets itself to 0.5, then to 0.75, etc.

avatar image Smurfj3 bogdankrstic · Mar 18, 2021 at 11:15 PM 0
Share

Decrease shouldn't change unless you change it somewhere in the code. From your explanation it seems its increasing .25 every "action" and so it should say Decrease += 0.25f; somewhere in your code, just get rid of that.

avatar image bogdankrstic Smurfj3 · Mar 21, 2021 at 07:45 PM 0
Share

Thanks dude, you're awesome!

avatar image
0

Answer by bogdankrstic · Mar 16, 2021 at 12:02 AM

Hi! Thank you for your answer. Yes, I assign the values for my public floats in the Inspector. I'll try the timer method and let you know how it goes.

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

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

Yield return inside a loop slowdown problem 2 Answers

Run Coroutine once, but finish Lerping 2 Answers

How to ''stack'' coroutines, and call each one till all are executed? 5 Answers

How to use Coroutines in C++/CLI? 1 Answer

While loop out of control...maybe? I'm not sure. Trying to track Coroutines progress. 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