• 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 /
  • Help Room /
avatar image
1
Question by kearley01 · Sep 30, 2015 at 12:01 PM · gameobject

How to increase and decrease object scale over time?

Hi everyone,

I'm trying to set up a scene where a pair of lungs are mimicking the process of breathing by increasing and decreasing their scale. What I want to do is increase the scale for 2 seconds and then decrease for 2 seconds and have this on some form of loop. Does that make sense? I know it has something to do with transform.localscale, but can't seem to get the timing right for c#.

Any help would be great!

Thanks so much :)

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by mostafa_hosseinzade · Apr 19, 2018 at 08:51 PM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Scale : MonoBehaviour {

 private int toggle;

 void Start () {
     toggle = 1;
  
 }
 
 // Update is called once per frame
 void FixedUpdate () {

     if (toggle >= 0 && toggle <= 100)
     {
         toggle += 1;
         transform.localScale += new Vector3(1, 1, 0);
     }
     else if (toggle < 0)
     {
         toggle += 1;
         transform.localScale -= new Vector3(1, 1, 0);
     }
     else {
         toggle = -100;
     }
 }
    

}

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
avatar image
12

Answer by Denvery · Sep 30, 2015 at 03:45 PM

You can use this code for more smooth animation:

 private float _currentScale = InitScale;
 private const float TargetScale = 1.1f;
 private const float InitScale = 1f;
 private const int FramesCount = 100;
 private const float AnimationTimeSeconds = 2;
 private float _deltaTime = AnimationTimeSeconds/FramesCount;
 private float _dx = (TargetScale - InitScale)/FramesCount;
 private bool _upScale = true;
 private IEnumerator Breath()
 {
     while (true)
     {
         while (_upScale)
         {
             _currentScale += _dx;
             if (_currentScale > TargetScale)
             {
                 _upScale = false;
                 _currentScale = TargetScale;
             }
             Lungs.localScale = Vector3.one * _currentScale;
             yield return new WaitForSeconds(_deltaTime);
         }
 
         while (!_upScale)
         {
             _currentScale -= _dx;
             if (_currentScale < InitScale)
             {
                 _upScale = true;
                 _currentScale = InitScale;
             }
             Lungs.localScale = Vector3.one * _currentScale;
             yield return new WaitForSeconds(_deltaTime);
         }
     }
 }

 private void Start()
 {
     StartCoroutine(Breath());
 }

Alternatively, you can create Animation with Mecanim Animation System, but it can be more complicate for you.

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 kearley01 · Sep 30, 2015 at 04:57 PM 1
Share

That's brilliant! Is there anyway to use this script and only affect the x and y axes? $$anonymous$$eeping the z value at 1f the entire time?

avatar image Denvery kearley01 · Sep 30, 2015 at 05:00 PM 2
Share

Z affects together with X & Y. Do you need to keep Z = 0? Just write Vector2.one ins$$anonymous$$d of Vector3.one . If you need to keep Z = 1, please write new Vector3(_currentScale, _currentScale, 1) ins$$anonymous$$d of Vector3.one * _currentScale;

avatar image Denvery kearley01 · Sep 30, 2015 at 05:39 PM 0
Share

if my answer helped you, please accept and/or vote it up

avatar image
4

Answer by Suddoha · Sep 30, 2015 at 12:53 PM

A Coroutine suits well for this purpose, if you need to implement that with a script and want the lungs to resize over time. Another way could be an animation which would probably look much nicer.

But here's a basic script, attach it to your gameObject and do not forget to set the public variables in the inspector (or make them private and assign a default value). This script assumes you're having a default scale of (1,1,1)

  • maxsize determines the max. scale

  • growFactor determine how fast it will scale, you could implement another float for breathing out faster if you want to

  • waitTime will make the lung wait between breathing in and breathing out.

(A small hint: if you don't need the default collider which is attached to the object, remove it as this needs some extra performance)

 public class NewBehaviourScript : MonoBehaviour
 {
     public float maxSize;
     public float growFactor;
     public float waitTime;
 
     void Start()
     {
         StartCoroutine(Scale());
     }
 
     IEnumerator Scale()
     {
         float timer = 0;
 
         while(true) // this could also be a condition indicating "alive or dead"
         {
             // we scale all axis, so they will have the same value, 
             // so we can work with a float instead of comparing vectors
             while(maxSize > transform.localScale.x)
             {
                 timer += Time.deltaTime;
                 transform.localScale += new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
                 yield return null;
             }
             // reset the timer
 
             yield return new WaitForSeconds(waitTime);
 
             timer = 0;
             while(1 < transform.localScale.x)
             {
                 timer += Time.deltaTime;
                 transform.localScale -= new Vector3(1, 1, 1) * Time.deltaTime * growFactor;
                 yield return null;
             }
 
             timer = 0;
             yield return new WaitForSeconds(waitTime);
         }
     }
 }







Comment
Add comment · Show 2 · 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 Wicaodian · May 25, 2016 at 07:20 AM 0
Share

Thank You So $$anonymous$$uch worked totally fine

avatar image mayankela21 · Oct 25, 2019 at 06:40 AM 0
Share

I not Understood maxSize is Vector3 or flot

avatar image
0

Answer by YoungDeveloper · Sep 30, 2015 at 12:21 PM

This will get you going. Create a cube and assign this script, then change the value from inspector while in play mode, you will see the cube will scale in all axis appropriately.

 public class PrototypeScaler : MonoBehaviour {
 
     public float value = 1f; //1 by default in inspector
 
     //This method is executed every frame
     private void Update(){
         //we store scale of this transform in temporary variable
         Vector3 temp = transform.localScale; 
 
         //We change the values for this saved variable (not actual transform scale)
         temp.x = value;
         temp.y = value;
         temp.z = value;
 
         //We assign temp variable back to transform scale
         transform.localScale = temp;
     }
 }

The storing in temporary variable is needed because you can't edit axis directly in c#, like transform.localScale.x = 2f is not allowed.

Comment
Add comment · Show 2 · 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 FeelsLikeGameDev · Jun 20, 2018 at 05:10 PM 1
Share

I tried that but it didn't quite work. $$anonymous$$aybe because of newer version of Unity. Here is what I came up with to make a simple scaling over time, maybe it helps someone.

     public float scalingFactor = 1.1f;
 
     void Update ()
     {        
         transform.localScale = new Vector3(transform.localScale.x+transform.localScale.x*scalingFactor*Time.deltaTime, transform.localScale.y+transform.localScale.y*scalingFactor * Time.deltaTime, transform.localScale.z+transform.localScale.z*scalingFactor * Time.deltaTime);
     }





avatar image Hoorza FeelsLikeGameDev · Dec 03, 2018 at 07:04 PM 0
Share

Works like a charm! Simple as well. Cheers. No need to write a bible to scale a carrot.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

C# - using getcompenent 1 Answer

How to get the position of a gameObject based on its index in an array? 0 Answers

If you want to destroy the game object, please call 'Destroy' on the game object instead. 1 Answer

Tips on improving instantiate performance? 1 Answer

How clone prefab and change it with not change original 0 Answers


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