• 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 Caiuse · Jul 23, 2011 at 01:58 PM · localscale

Increase and decrease localScale once over

Okay so I have an object the pulsing in size over time, But I just want it to pulse up and down once over

     var phi : float = Time.time / duration * 2 * Mathf.PI;
     var amplitude : float = Mathf.Cos( phi ) * 0.5 + 0.5;
     transform.localScale = Vector3(amplitude,amplitude,amplitude);

Any suggestions, Thanks - C

Comment
Add comment · Show 2
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 BerggreenDK · Jul 23, 2011 at 02:50 PM 0
Share

"I just want it to pulse up and down once over" ??? you mean, no loop?

avatar image Caiuse · Jul 23, 2011 at 02:54 PM 0
Share

Yes sorry for the wording, I would like to scale up, and scale down once over.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BerggreenDK · Jul 23, 2011 at 03:19 PM

If I understand your question correct, you are looking for a way to make your object go up and down in size once, right?

Take a look at how Sinus/Cosinus is related: http://upload.wikimedia.org/wikipedia/commons/8/82/Sinus_en_cosinus.png

All you need it so figure out where to start and end with the phi value.

instead of Time.time that keeps running, change this value into your own float.

Update with example showing how Sinus works:

     float result = 0f;
     
     for (float deg = 0.0f; deg < 360.0f ; deg +=5f)
     {
         result = Mathf.Sin( deg * Mathf.Deg2Rad ) ;
         
         Debug.Log("Degrees (" +  deg + ") = " + Mathf.Round ( result *1000 )/1000 );
     }


I count from 0 to 360 (a full circle) as you might see 90 degress, 180 degress and 270 are very interesting for your value.

Now, take the function and multiply it result that goes from 0 to 1 and back with your scale size. That value you can use instead of amplitude.

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 Caiuse · Jul 23, 2011 at 03:32 PM 0
Share

I'm not quiet with you, I've tried changing the Phi value you my own float, but then the object doesn't scale at all :/

avatar image BerggreenDK · Jul 24, 2011 at 02:05 PM 0
Share

was this usefull or ?

avatar image
0

Answer by Owen-Reynolds · Jul 23, 2011 at 05:15 PM

The main observations are (very easy trig and algebra):

1) to get one pulse, run for either 180 or 360 degrees, instead of all the time.

2) depending starting angle, you could go down/up, up/down, snap up then go down, ... so pick the starting angle correctly, using a sin/cos chart.

3) cos/sin thinks 0 degrees is aimed to the right, and goes counter-clockwise in radians. There are 2*PI radians (6.28) in a circle. If you try to use 0-360, you get crazy fast spins. Other things in Unity use degrees and 0 is up, but cos/sin uses "real" math.

4) Sin/Cos go between -1 and 1. Mathf.Cos( phi ) * 0.5 + 0.5; (from your code snippet,) scales/slides the range to go from 0 to 1. If you used 2+3, it would go from 1 to 5. The + is the center and the is the amount +/- it moves.

This C# code worked for me:

// globals: float growEndSecs = 0; float GROW_TOTAL_SECS = 2.0f;

// sample line to test: if(Input.GetKeyDown("z")) growEndSecs = Time.time + GROW_TOTAL_SECS; // do this to start growing

// are we currently growing? if(Time.time < growEndSecs) { // pct goes from 0 to 1: float pct = 1 - (growEndSecs - Time.time)/GROW_TOTAL_SECS; // scale to start ang at -90 (-PI/2), so sin starts at lowest value and goes up: float ang = pct 2*Mathf.PI - Mathf.PI/2.0f; // Sin(ang) goes from -1 to 1 back down to -1. Scale to go from 1 to 2 to 1: float scaleFact = Mathf.Sin(ang) 0.5f + 1.5f; // NOTE: this ranges from 1.5 +/- 0.5, so 1 to 2 // to get 1 to 3, for example, use 1+2 transform.localScale = Vector3.one scaleFact; }

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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

clamp limit variables trouble 1 Answer

transform.localScale unexpected behavior after Rotate() 1 Answer

How to stop scale reducing from reaching Zero (Solved) 1 Answer

Scale objects marginally! 0 Answers

Scale (Transform.localscale) a gameobject based on ARFaceAnchor anchorData 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