- Home /
Oscillating variable
How can i get the value of a variable to oscillate over time using Javascript?
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.
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!
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.
I will add an example to this answer which oscillates around its original position.
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.
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.
NV$$anonymous$$, i figured it out. Just had to change transform.Position to transform.localPosition
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.
While these are really useful in their own right, they don't perform the task of oscillating a value over time.
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.
The link given in this (by now very old) answer now directs the user to a malware site :(
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;
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
Answer by JoshOClock · Dec 17, 2010 at 10:08 PM
I posted an answer for a different question some how...
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.
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);
Your answer
