- Home /
How to make a sine wave with a transform
Transform should move as a wave.
How to do it in c#?
@sonaviswam, you get downvotes because you asked a bad question ( in this case, I can see that you have done nothing at all in your effort to solve the problem).
Ironically, this is now the top Google result for sine waves in unity, and everyone can see you have done nothing to solve this problem.
void Update () {
firstPos = floorPos + new Vector3(0f, 0.8f, 0f);
lastPos = floorPos + new Vector3(0f, 0f, 0f);
lerp = $$anonymous$$athf.PingPong(Time.time, duration) / duration;
transform.position = Vector3.Lerp(firstPos, lastPos, lerp);
}
These much i tried. It wont move x positions. i need that also
my charcter is flying in sky. obstacle should move as above image from left to right and then right to left contiously with changing y position also. within limit point. until charcter escape from that. i searched in google so much. But not getting answer.
you're not getting a good solution because you're at the point where you have all the information you need to solve your specific problem on your own. you need to now apply logic to the information you've been given.
the sin function produces movement between + and - one. multiply it by the width of your zone, add the center of your zone, and you're done.
transform.position = Vector3( center.x + $$anonymous$$athf.Sin(Time.time) * size.x, ...y..., ...z...);
basic things like value sinf got it from unity site. but logic didnt get . being new to unity it is difficult for me
Answer by fafase · Apr 09, 2013 at 09:31 AM
Use Mathf.Sin. Ok you want the guy to go left and right AND up and down. Now it looks like the guy is somehow "bouncing" at the bottom instead of having a rounded turn.
So!!! You need to use a cos function on the y axis but as absolute value to create the sharp shape at the bottom. Then you use another cos function on the x axis so that it moves left and right. But first, create an empty game object that you place where you want this to happen. Then add your moving object as child to it.
Then use the code below on the child object.
float amplitudeX = 10.0f;
float amplitudeY = 5.0f;
float omegaX = 1.0f;
float omegaY = 5.0f;
float index;
public void Update(){
index += Time.deltaTime;
float x = amplitudeX*Mathf.Cos (omegaX*index);
float y = Mathf.Abs (amplitudeY*Mathf.Sin (omegaY*index));
transform.localPosition= new Vector3(x,y,0);
}
Now your object is going left to right to left and so on, but also up and down with the bouncing effect at the bottom.
Now it is up to you to modify amplitude to define how large is the movement and omega defines how fast is the movement. If you want to remove the bouncing, remove the Mathf.Abs.
it is a simple Vector3
to move the gameObject sideway so that it wouldn't looked like it is jumping up and down and more like following a sine wave path.
If anyone is looking for a normal Sine wave solution for making an object float up and down, this might help a little:
void SmoothSineWave () {
// y(t) = A * sin(ωt + θ) [Basic Sine Wave Equation]
// [A = amplitude | ω = AngularFrequency ((2*PI)f) | f = 1/T | T = [period (s)] | θ = phase | t = elapsedTime]
// Public/Serialized Variables: amplitude, period, phase
// Private/Non-serialized Variables: frequency, angularFrequency, elapsedTime
// Local Variables: omegaProduct, y
// If the value of period has altered last known frequency...
if (1 / (period) != frequency) {
// Recalculate frequency & omega.
frequency = 1 / (period);
angularFrequency = (2 * $$anonymous$$athf.PI) * frequency;
}
// Update elapsed time.
elapsedTime += Time.deltaTime;
// Calculate new omega-time product.
float omegaProduct = (angularFrequency * elapsedTime);
// Plug in all calculated variables into the complete Sine wave equation.
float y = (amplitude * $$anonymous$$athf.Sin (omegaProduct + phase));
//
transform.localPosition = new Vector3 (0, y, 0);
}
Answer by iMagesBlues · Jun 24, 2016 at 05:06 AM
A no code-solution is to use Mecanim. Draw a sine-wave graph for the transform's y-position variable in the Animation Window > Curves.