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
1
Question by littlebigprogramming · Jul 05, 2017 at 08:42 AM · c#scalelerplocalscale

How to Scale an object up and down over time?

Hi,

I have this code that should scale an object up and down over a series of time.

My problem is however that it reaches its max scale and does not set the boolean to switch the statement to scale down.

 if (transform.localScale.x < mMinScale.x && !mScaleUp) // 
    {
             transform.localScale = Vector3.Lerp(transform.localScale, -mMinScale, 2 * Time.deltaTime);
             print("HELLO");
    }
     if (transform.localScale.x < mMaxScale.x && mScaleUp)
     {
         
         transform.localScale = Vector3.Lerp(transform.localScale, mMaxScale, 2 * Time.deltaTime);
         print("GOODBYE");
     }
     if (transform.localScale.x == mMaxScale.x)
     {
         mScaleUp = false;
         print(mScaleUp);
     }


Any ideas?

Kind Regards

New code is

     if (transform.localScale.x > mMinScale.x && !mScaleUp) 
     {
             transform.localScale = Vector3.Lerp(transform.localScale, -mMinScale, 2 * Time.deltaTime);
             print("HELLO");
     }
     if (transform.localScale.x < mMaxScale.x && mScaleUp)
     {
         // transform.localScale = //Time.deltaTime
         transform.localScale = Vector3.Lerp(transform.localScale, mMaxScale, 2 * Time.deltaTime);
         print("GOODBYE");
     }
     if (transform.localScale.x >= mMaxScale.x)
     {
         mScaleUp = false;
         print(mScaleUp);
     }
 
     else if (transform.localScale.x <= mMinScale.x)
     {
         mScaleUp = true;
         print(mScaleUp);
     }

Comment
Add comment · Show 1
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 littlebigprogramming · Jul 04, 2017 at 08:31 PM 0
Share

maxScale is 1 but the highest the transform gets to is 0.9999992

5 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by RicardoAntezana · Aug 10, 2017 at 11:43 AM

Hey!

Why not try using the Sin function to create a repetitive motion such as a pulsating object? I feel this is a perfect opportunity to use one!

In this example script we will have your transform object to have its size move from 1 to -1:

 using UnityEngine;
 
 public class Scaling : MonoBehaviour {
 
     void Update ()
     {
         Vector3 vec = new Vector3(Mathf.Sin(Time.time), Mathf.Sin(Time.time), Mathf.Sin(Time.time));
 
         transform.localScale = vec;
     }
 }

If you do not want your object to go from -1 or 1 then you can add numbers to your sin function like so:

 using UnityEngine;
 
 public class Scaling : MonoBehaviour {
 
     void Update ()
     {
         Vector3 vec = new Vector3(Mathf.Sin(Time.time) + 1, Mathf.Sin(Time.time) + 1, Mathf.Sin(Time.time) + 1);
 
         transform.localScale = vec;
     }
 }

That script will make your object grow from 0 to 2 and then back again.

You can change the speed at which it pulsates by multiplying your time variable by some number, for example:

 using UnityEngine;
 
 public class Scaling : MonoBehaviour {
 
     void Update ()
     {
         Vector3 vec = new Vector3(Mathf.Sin(Time.time * 2), Mathf.Sin(Time.time * 2), Mathf.Sin(Time.time * 2));
 
         transform.localScale = vec;
     }
 }

That script would make it pulsate at twice the speed! Aren't Sin functions great? You could also make it 0.5 or a smaller fraction to make it pulsate slower.

You can change the amplitude of your cube by multiplying the entire function by some number, for example:

 using UnityEngine;
 
 public class Scaling : MonoBehaviour {
 
     void Update ()
     {
         Vector3 vec = new Vector3(2 * Mathf.Sin(Time.time), 2 * Mathf.Sin(Time.time), 2 * Mathf.Sin(Time.time));
 
         transform.localScale = vec;
     }
 }

That script would make your transform pulsate from -2 to 2. If you were to multiply it by 0.5 or a smaller fraction, it would pulsate at a smaller pace.

Here is a great video to learn more about Sin functions: https://www.youtube.com/watch?v=pEXdTLsEAjk

Thanks, I hope you learned something and good luck with your programming endeavours!

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 Musabbir · Jul 21, 2018 at 06:28 AM 1
Share

This one is the best answer. Thanks man

avatar image
2

Answer by FunIsDangerous · Jul 04, 2017 at 02:10 PM

This seems to work perfectly. If you dont want it to scale on a certain axis, simply set the scaling speed on that axis to 0.

 [Header("Max scale")]
 public Vector3 mMinScale;
 [Header("Min scale")]
 public Vector3 mMaxScale;
 [Header("Scaling Speed")]
 public Vector3 mScaleSpeed;
 
 private Vector3 currentScaleSpeed;
 
 void Start()
 {
     currentScaleSpeed = mScaleSpeed;
 }
 
 void Update()
 {
     if (transform.localScale.x <= mMinScale.x || transform.localScale.x >= mMaxScale.x)
     {
         currentScaleSpeed.x *= -1; //invert it
     }
 
     if (transform.localScale.y <= mMinScale.y || transform.localScale.y >= mMaxScale.y)
     {
         currentScaleSpeed.y *= -1; //invert it
     }
 
     if (transform.localScale.z <= mMinScale.z || transform.localScale.z >= mMaxScale.z)
     {
         currentScaleSpeed.z *= -1; //invert it
     }
 
     transform.localScale = transform.localScale + currentScaleSpeed * Time.deltaTime;
 }
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 littlebigprogramming · Jul 04, 2017 at 05:43 PM 0
Share

Thanks for your comment I'll try to implement this tomorrow :)

avatar image
0

Answer by Saryk360 · Jun 22, 2017 at 03:13 PM

Change "==" to ">=" on line 12.

Comment
Add comment · Show 4 · 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 littlebigprogramming · Jun 22, 2017 at 03:17 PM 0
Share

No change, does exactly the same. I understand why you suggested that though. I'll keep it that way in the eventuality it is >= than. :)

avatar image Saryk360 littlebigprogramming · Jul 04, 2017 at 01:31 PM 0
Share

Did you manage to fix the issue ?

avatar image littlebigprogramming Saryk360 · Jul 04, 2017 at 05:40 PM 0
Share

Hey,

I never managed to get it to work, I tried adding a statement like your comment below but I couldn't get it to work.

I understand the logic you was trying to add but I'm lost as to why it doesn't work I benched it for a while but I will try and pick it up again tomorrow.

$$anonymous$$any Thanks

avatar image Saryk360 · Jul 04, 2017 at 01:34 PM 0
Share

So basically you should change the "<" on line 1 to a ">", the "==" to a ">=" on line 12, add a statement to reset mScaleUp to true when "transform.localScale.x <= m$$anonymous$$inScale.x", and then I don't think there's any reason for it not to work :)

avatar image
0

Answer by Manpreet_96 · Jun 22, 2017 at 05:42 PM

On line 1, change "if (transform.localScale.x < mMinScale.x && !mScaleUp)" to "if (transform.localScale.x > mMinScale.x && !mScaleUp)".. Rest looks fine, it should work.. @littlebigprogramming

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 littlebigprogramming · Jun 23, 2017 at 04:49 PM 0
Share

Tried that also, sadly it made no difference. Thanks for the suggestion. :)

avatar image Manpreet_96 · Jun 23, 2017 at 04:59 PM 0
Share

@littlebigprogram$$anonymous$$g To the best of my knowledge, your both if statements are being run. Change the condition in first "if" as i suggested before and change the second "if" statement to "else if". Do comment if this works, otherwise I have another approach too in $$anonymous$$d but that would be a bit lengthy. $$anonymous$$eep Chillin!!! :D

avatar image
0

Answer by NiaG-A · Aug 19, 2017 at 08:49 AM

I made a tutorial for this on my YouTube channel https://www.youtube.com/watch?v=Wymchsif758 it only requires a couple lines of 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

336 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 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 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 to use WaitForSeconds inside IF / Else If statement? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Scale Object Using Lerp? 2 Answers

problem Changing Scales in Script -C# 4 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