How to make a sine wave with a transform

Transform should move as a wave.

How to do it in c#?

9875-wave.png

@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).

void Update () { firstPos = floorPos + new Vector3(0f, 0.8f, 0f); lastPos = floorPos + new Vector3(0f, 0f, 0f); lerp = Mathf.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 + Mathf.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

2 Answers

2

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.

@fafase:Thanks. Its working fine.

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.