2

I want to create a Sine Wave generated from a unit circle in Unity whose amplitude and frequency can be dynamically changed by changing the speed and radius of the circle. Ex: enter image description here

The result should look something like this(see pictures below) where one continuous curve shows the sustained changes in frequency and amplitude when looked over time: enter image description here enter image description here

This is how I'm currently approaching the problem to produce Sine wave (this script is not linked to the circle right now) but it doesn't allow for the older values to be sustained overtime..

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class generateSineWave : MonoBehaviour {

    [Range(0.1f,20.0f)]
    public float heightScale = 5.0f;
    [Range(0.1f,40.0f)]
    public float detailScale = 5.0f;

    public GameObject wavePlane;

    private Mesh myMesh;
    private Vector3[] vertices;
    /*
    //Unit Circle properties
    public Transform target;
    public float orbitDistance = 1.0f;
    public float orbitDegreesPerSec = 90.0f;
    public GameObject Ball;
    */

    void Update()
    {
        GenerateWave();
    }

    void GenerateWave()
    {
         myMesh = wavePlane.GetComponent<MeshFilter>().mesh;
         vertices = myMesh.vertices;

         int counter = 0; //i
         int yLevel = 0; //j

        for (int i = 0; i < 11; i++)
        {
            for (int j = 0; j < 11; j++)
            {
                CalculationMethod(counter, yLevel);
                counter ++;
            }
            yLevel ++;
        }

        myMesh.vertices = vertices;
        myMesh.RecalculateBounds();
        myMesh.RecalculateNormals();

        Destroy(wavePlane.GetComponent<MeshCollider>());
        MeshCollider collider = wavePlane.AddComponent<MeshCollider>();
        collider.sharedMesh = null;
        collider.sharedMesh = myMesh;

    }

    //public bool waves = false;
    //public float waveSpeed = 5.0f;

    void CalculationMethod(int i, int j)
    {
                    //radius * Mathf.Sin(speed* Time.time + vertices[i].x + phase shift)
        vertices[i].z = Mathf.Sin(Time.time + vertices[i].x);
        vertices[i].z -=j;

    }

}

I looked at the Unity Asset Store, there are a few wave generators, but none of them work with varying amplitude and frequency such that the resulting curve is one continuous curve where previous amplitudes and frequency still show in the editor.

Pratham Sehgal
  • 143
  • 1
  • 5
  • 17
  • @Code-Apprentice With the radius and angle. Ex. https://www.youtube.com/watch?v=a_zReGTxdlQ – Pratham Sehgal Feb 21 '18 at 20:05
  • 1
    What is your question? – pookie Feb 21 '18 at 22:31
  • @pookie How to make a Sine wave generated from a unit circle like I explained above in Unity? – Pratham Sehgal Feb 22 '18 at 05:43
  • *"Questions asking us to recommend or find a book, tool, software library, **tutorial** or other off-site resource are [off-topic](https://stackoverflow.com/help/on-topic) for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it."* – Zze Feb 22 '18 at 05:49
  • 1
    @Zze I added my current approach and where I'm stuck right now.. Lmk if that helps improve quality of the question. – Pratham Sehgal Feb 22 '18 at 06:12

2 Answers2

0

I don't quite understand how you're going to use generator, but you can just calculate next value for your graph or whatever on update.

void Generate(double previousPositionOnCircleInRadian, double speed,
    double amplitude, out double sinValueMultiliedByAmplitude, 
    out double nextPositionOnCircleInRadian)
{
    nextPositionOnCircleInRadian = previousPositionOnCircleInRadian + Time.deltaTime*speed;
    sinValueMultiliedByAmplitude = amplitude * Math.Sin(nextPositionOnCircleInRadian);
}

I guess it's all you need to calculate position on your circle and sinwave.

Pavlo Holotiuk
  • 230
  • 2
  • 10
0

I have an example of Sine Wave Code... but I'm not sure if it's what you need.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class SinewaveController : MonoBehaviour
{
    public float amplitude = 1f; // Amplitude of the sinewave
    public float frequency = 1f; // Frequency of the sinewave
    public float speed = 1f; // Speed of the sinewave

    private Vector3 startPosition; // Starting position of the object
    private float time = 0f; // Time elapsed since start

    private void Start()
    {
        startPosition = transform.position; // Store the starting position of the object
    }

    private void Update()
    {
        time += Time.deltaTime * speed; // Update the time elapsed

        // Calculate the new position based on the sinewave equation
        float yPosition = amplitude * Mathf.Sin(2f * Mathf.PI * frequency * time);
        Vector3 newPosition = startPosition + new Vector3(0f, yPosition, 0f);

        transform.position = newPosition; // Set the new position of the object
    }
}

It takes an input and outputs a transform... I know it isn't what you want, but it might help you realize something you didn't realize.

Anosan
  • 1
  • 1
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 27 '23 at 10:05