Creating a sine wave

I am trying to create a wave-like motion along a line of points. (Vector3).

#pragma strict

var waterScale : float = 5.0;

var space : float = 0.5;

var accuracy : int = 20;

var x : float = 5.0;

var height : float = 0;
private var points : Vector3[];

function Update ()
{
    height = Mathf.Sin(Time.time) * waterScale;
	Wave();
	points = new Vector3[accuracy];
}

function Wave()
{
	for(var i = 0; i < points.Length - 3; i++)
	{
		points[i].x = space * i;
		points[i].z = height;
		
		if(i == points.Length)
		{
			i = 0;
		}
		
		yield WaitForSeconds(0.01);
	}
}

function OnDrawGizmos()
{
	Gizmos.color = Color.white;
	
	for(var i = 0; i < points.Length; i++)
	{
		Gizmos.DrawSphere(points[i], 0.2);
	}
}

However, right now all the spheres are moving up and down at the same rate, creating a line of points which just oscillates between two points, when I am trying to create something that looks like this:

Help?

Try this…!!! i just came up with this it gives you the wavy effect…

// Water tension is simulated by a simple linear convolution over the height field.
		for(int i=1; i<size-1; i++)
		{
			int j=i-1;
			int k=i+1;
			newHeight[i] = (vertex[i].transform.position.y + vertex[j].transform.position.y + vertex[k].transform.position.y) / 3.0f;
		}
		// Velocity and height are updated... 
		for(int i=0; i<size; i++)
		{
			// update velocity and height
			velocity[i] = (velocity[i] + (newHeight[i] - vertex[i].transform.position.y)) * velocityDamping;
			float timeFactor = Time.deltaTime * timeScale;
			if (timeFactor > 1f)
				timeFactor = 1f;
			newHeight[i] += velocity[i] * timeFactor;
			// update the vertex position
			Vector3 newPosition = new Vector3(vertex[i].transform.position.x,newHeight[i],vertex[i].transform.position.z);
			vertex[i].transform.position = newPosition;
		}
using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public Color c1 = Color.yellow;
    public Color c2 = Color.red;
    public int lengthOfLineRenderer = 20;
    void Start() {
        LineRenderer lineRenderer = gameObject.AddComponent<LineRenderer>();
        lineRenderer.material = new Material(Shader.Find("Particles/Additive"));
        lineRenderer.SetColors(c1, c2);
        lineRenderer.SetWidth(0.2F, 0.2F);
        lineRenderer.SetVertexCount(lengthOfLineRenderer);
    }
    void Update() {
        LineRenderer lineRenderer = GetComponent<LineRenderer>();
        int i = 0;
        while (i < lengthOfLineRenderer) {
            Vector3 pos = new Vector3(i * 0.5F, Mathf.Sin(i + Time.time), 0);
            lineRenderer.SetPosition(i, pos);
            i++;
        }
    }
}

Just a point to consider : the OP is working in uJS, and C# examples may confuse unless specified as C#.

I have answered the initial post here : http://forum.unity3d.com/threads/187151-Creating-a-plane-from-an-array-of-Vector3s-2D-Water

  offset = waveHeight * Mathf.Sin( ( Time.time * waveSpeed ) + ( i * waveFrequency ) );
1 Like

Thanks to everyone here, especially alucardj, he noticed I posted this twice hahah :slight_smile:

Thanks guys.

Just so you know, the picture you shown of Sine Wave is actually only moving in 1 dimension in space. So just moving up and down would be correct. You meaned to create an effect similar to the picture but what you need is to change a sine value in 1 dimension and increase/decrease a value in the direction you want to move forward.

Both guys who answered already stated this in the “codes” they provided, but they did not let you know you are misinformed about Sine function.
Good day!