• 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
1
Question by lakam · Jan 19, 2014 at 01:22 AM · javascriptif-statementsscalingloopingwhile-loop

How to gradually grow and shrink an object

Alright so I'm pretty new at Unity and actually started recently. Along with Unity I have started learning javascript. So, I have a Unity project in which there is a scene with a single cube. I attempted to make some code with which the cube will gradually grow until it's x scale reaches 775. After that, it will gradually shrink back to 1. So far I have succeeded in making the cube gradually grow, however, I can't seem to make the gradual shrinking part work. Here is my code:

 var growthRate: int=1;
 public function grow()
 {
     transform.localScale.x=transform.localScale.x+growthRate;
     transform.localScale.y=transform.localScale.y+growthRate;
     transform.localScale.z=transform.localScale.z+growthRate;
 }
 
 public function shrink()
 {
     transform.localScale.x=transform.localScale.x-growthRate;
     transform.localScale.y=transform.localScale.y-growthRate;
     transform.localScale.z=transform.localScale.z-growthRate;
 }
 
 public function main()
 {
     if(transform.localScale.x!=775)
     {
         grow();
     }
     else
     {
         while(transform.localScale.x!=1)
         {
             shrink();
         }
     }
 }

 
 function Update() {
     
     main();
     
 }


Earlier, I attempted to put a while and a for loop in the Update function, because I knew this would be the simplest in means of gradually increasing a value. However, that didn't seem to be the case. So I tried using if statements instead...The gradual growth part worked, but when it reached it's maximum of 775, it would simply shrink down to 774 and then go back to 775. It looped forever after that. So now, for my shrink function, I'm using a while statement so that the cube will actually shrink down to it's usual size. However, the only problem with the while loop is that it doesn't make the shrinking gradual. Instead, the shrinking happens instantly. How can I fix this?

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
2

Answer by KellyThomas · Jan 19, 2014 at 02:09 AM

This logic is faulty:

 if(transform.localScale.x!=775)
 {
    grow();
 }
 else
 {
    while(transform.localScale.x!=1)
    {
      shrink();
    }
 }

It would the scale would increase at the rate of 1/frame. Then if it became equal to 775 it would enter the while loop and head back down to 1 all in on frame.

Given the imprecision of floats it is not guaranteed to actually register with either guard check.

It is usually better to check if a float exceeds the given bounds. For this type of back-and-forth type of behaviour it is also require to somehow remember which way you are going.

Here is one possible implementation:

 var scaleRate: float = 1.0f;
 var minScale: float = 775.0f;
 var maxScale: float = 1.0f;
 
 public function ApplyScaleRate() {
     transform.localScale += Vector3.one * scaleRate;
 }
  
 public function main() {
     //if we exceed the defined range then correct the sign of scaleRate.
     if(transform.localScale.x < minScale) {
         scaleRate = Mathf.Abs(scaleRate);
     }
     else if(transform.localScale.x > maxScale) {
         scaleRate = -Mathf.Abs(scaleRate);
     }
     ApplyScaleRate();
 }
  
 function Update() {
     main();
 }


Note: I haven't used it here but you may like to look at Mathf.PingPong when building this type of back-and-forth animation.

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
0

Answer by ematsuno · Mar 08, 2019 at 12:34 AM

transform.localScale.x = .5f; Error: Cannot modify the return value of 'Transform.localScale' because it is not a variable

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 KellyThomas · Mar 09, 2019 at 03:18 AM 0
Share

This should explain what you need to do.

https://gamedev.stackexchange.com/questions/90359/why-cant-i-store-a-value-in-transform-position-y

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 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

Related Questions

Unity freezes after running this basic code. 1 Answer

How could I do this as simple as possible? 2 Answers

How to get something to be inactive while there is a certain value 2 Answers

While loop won't stop crashing!!! 1 Answer

Why is this script I made crashing unity 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges