Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
8
Question by Mattivc · Dec 08, 2009 at 10:12 AM · math

Oscillating variable

How can i get the value of a variable to oscillate over time using Javascript?

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 runevision ♦♦ · Feb 17, 2010 at 01:09 PM 0
Share

I removed the javascriptspecific tag, since there's no difference in how you'd make a variable oscillate in Javascript or C# (besides some syntax), so the question is relevant to others regardless of language choice.

6 Replies

· Add your reply
  • Sort: 
avatar image
24
Best Answer

Answer by duck · Dec 08, 2009 at 10:18 AM

There are two functions which can do this.

If you want the value to oscillate in a smooth curve (like a Sine wave), use Mathf.Sin

If you want the value to oscillate linearly back and forth between two values (like a triangle wave), use Mathf.PingPong

PingPong will return a value between zero and your specified maximum, while Sin will return a value between -1 and 1.

Whichever you choose, you'll want to feed in the value from Time.time, and optionally multiply it up or down depending on how fast you want the value to oscillate. Eg:

// example using PingPong function Update () { // Set the x position to loop between -3 and 3 transform.position.x = Mathf.PingPong(Time.time, 6) - 3; }

// example using Sin function Update () { // Set the x position to loop between -3 and 3 transform.position.x = Mathf.Sin(Time.time) * 3;

 // Set the y position to loop much faster between -3 and 3
 transform.position.y = Mathf.Sin(Time.time * 5) * 3;

}

And an example which oscillates the object around its original position (as requested in the comments below):

var originalPosition : Vector3;

function Start () { // when the object starts, we record its initial position originalPosition = transform.position; }

function Update () { // when repositioning the object, we add an offset to the original position transform.position.x = originalPosition.x + Mathf.Sin(Time.time) * 3; }

enjoy!

Comment
Add comment · Show 6 · 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 Mattivc · Dec 08, 2009 at 02:00 PM 0
Share

When i try this the object suddenly moves to one of edges of the scene. As its start position is at 223 in the X axis. How can i make it oscillate around its original placement.

avatar image duck ♦♦ · Dec 08, 2009 at 03:26 PM 0
Share

I will add an example to this answer which oscillates around its original position.

avatar image Novodantis 1 · Dec 08, 2009 at 03:27 PM 0
Share

Store the original position on the Awake() call then add it to the right hand side of these calculations [eg. transform.position.x = originalPosition.x + ($$anonymous$$athf.Sin(Time.time) * 3);]. Alternatively you can use transform.Translate, although the movement values will need to be tweaked as it will be a curve on acceleration, rather than speed.

avatar image Mattivc · Dec 09, 2009 at 08:11 AM 0
Share

Great, i have got it working now. But there is one more thing i cant seem to figure out about it. The script is making it oscillate acording to World space, how can i make it oscilate according to its own local location and rotation.

avatar image Mattivc · Dec 09, 2009 at 10:54 AM 0
Share

NV$$anonymous$$, i figured it out. Just had to change transform.Position to transform.localPosition

Show more comments
avatar image
4

Answer by jashan · Dec 08, 2009 at 10:45 AM

You may also want to have a look at Mathfx on the Unifycommunity Wiki

This provides:

  • Hermite
  • Sinerp
  • Coserp
  • Berp
  • Bounce
  • SmoothStep
  • NearestPoint
  • NearestPointStrict

Source code is available both in C# and JavaScript - and on the page on the Wiki, you can also find graphs of the different methods.

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 duck ♦♦ · Dec 08, 2009 at 11:01 AM 0
Share

While these are really useful in their own right, they don't perform the task of oscillating a value over time.

avatar image jashan · Dec 09, 2009 at 07:35 AM 0
Share

Technically, that's correct: $$anonymous$$athfx doesn't provide a method for "oscillating over time". However, someone looking for one of the effects provided by $$anonymous$$athfx may very well try a search with "oscillating" in lack of a more technically appropriate term.

avatar image PrimalCoder · Mar 23, 2021 at 07:00 PM 0
Share

The link given in this (by now very old) answer now directs the user to a malware site :(

avatar image
3

Answer by castor · Jul 28, 2013 at 07:50 PM

In case you want to define what the actual values are I think this is the answer

     var startRange : float = 5;    //your chosen start value
         var endRange : float = 24;    //your chose end value
         var oscilationRange = (endRange - startRange)/2;
         var oscilationOffset = oscilationRange + startRange;
         
         result = oscilationOffset + Mathf.Sin(Time.time) * oscilationRange;
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 KarlKarl2000 · Nov 11, 2016 at 10:37 PM 0
Share

Thanks @Duck and @castor

This works as written! $$anonymous$$uch thanks!~~~~

for those using c#

     private float _endRange = 1;
     private float _startRange = 1.8f;
     private float _oscillateRange;
     private float _oscillateOffset;
 
 
     void Start () {
         _oscillateRange = (_endRange - _startRange) / 2;
         _oscillateOffset = _oscillateRange + _startRange;
     }
     
     void Update () {
         _fooBar  = _oscillateOffset + $$anonymous$$athf.Sin (Time.time * 0.35f) * _oscillateRange;
     }
 

Sharing is Caring https://twitter.com/IndieNendoroid

avatar image
0

Answer by JoshOClock · Dec 17, 2010 at 10:08 PM

I posted an answer for a different question some how...

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 SBBowen · Nov 18, 2012 at 01:31 AM

Does anyone know how to apply this to rotation? Substituting "rotation" for "position" in the above solution gives weird and useless results.

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 castor · Jul 30, 2013 at 07:05 AM 0
Share

can you post your code?

avatar image kskjadav007 · Aug 19, 2017 at 10:02 AM 0
Share

i need rotation tooo

// do the sinewave transform.position = pos + axis $$anonymous$$athf.Sin (Time.time frequency) magnitude; // and rotation transform.rotation = Quaternion.Euler (0,0,maxrotation +$$anonymous$$athf.Sin (Time.time*frequency)*magnitude);

  • 1
  • 2
  • ›

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Implementation behind Transform.forward 4 Answers

3D. How to get 1 pixel to equal 1 degree camera FOV 0 Answers

Making slider match health when values are different. 0 Answers

Connect matlab and unity 0 Answers

Finding Point on a Bounds (2D Rectangle) using its Centre and an Angle. 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