Don't like ads? PRO users don't see any ads ;-)

Unity Pulse Block Graph

By: notalentgeek on Jun 21st, 2012  |  syntax: C#  |  size: 2.02 KB  |  hits: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Graphing : MonoBehaviour {
  5.         public int resolution;
  6.         private int currentResolution;
  7.        
  8.         private ParticleSystem.Particle[] points;
  9.        
  10.         // Graph position of 800 * 600
  11.         private float graphX;
  12.         private float graphY;
  13.         private float graphZ;
  14.        
  15.         // Graph properties.
  16.         private float graphW;
  17.         private float graphH;
  18.         private float graphF;
  19.        
  20.         // Sine position.
  21.         private float sinePos;
  22.        
  23.         // Use this for initialization
  24.         void Start () {
  25.                 // Graph posiotion of 800 * 600
  26.                 graphX = 2.7f;
  27.                 graphY = 0.4f;
  28.                 graphZ = 0.0f;
  29.                
  30.                 // Edit initial position
  31.                 transform.position = new Vector3(graphX, graphY, graphZ);
  32.                
  33.                 CreatePoints();
  34.         }
  35.        
  36.         // Update is called once per frame
  37.         void Update () {
  38.                 // Graph properties
  39.                 graphW = GUI_Component.wHSliderValue; // Suitable value is 1.0f
  40.                 graphH = GUI_Component.hHSliderValue; // Suitable value is 20.0f
  41.                 graphF = GUI_Component.fHSliderValue; // Suitable value is 3.0f
  42.                
  43.                 // Default graph resolution
  44.                 resolution = (int)GUI_Component.gHSliderValue;
  45.                
  46.                 if(currentResolution != resolution){
  47.                         CreatePoints ();
  48.                 }
  49.                 for(int i = 0; i < resolution; i++){
  50.                         Vector3 p = points[i].position;
  51.                         p.y = Sine(p.x);
  52.                         points[i].position = p;
  53.                 }
  54.                 particleSystem.SetParticles(points, points.Length);
  55.         }
  56.        
  57.         private void CreatePoints(){
  58.                 if(resolution < 2){
  59.                         resolution = 2;
  60.                 }
  61.                 currentResolution = resolution;
  62.                 points = new ParticleSystem.Particle[resolution];
  63.                 float increment = 1f / (resolution - 1);
  64.                 for(int i = 0; i < resolution; i++){
  65.                         float x = i * increment;
  66.                         points[i].position = new Vector3(x, 0.0f, 0.0f);
  67.                         points[i].color = new Color(255.0f, 255.0f, 255.0f);
  68.                         points[i].size = 0.1f;
  69.                 }
  70.         }
  71.        
  72.         private float Sine(float x){
  73.                 float formula = Mathf.PI * x * graphW / graphF + Time.timeSinceLevelLoad;
  74.                 float sinFormula = Mathf.Sin(formula);
  75.                 if(sinFormula > 0){
  76.                         sinFormula = 1;
  77.                 }
  78.                 else if(sinFormula < 0){
  79.                         sinFormula = -1;
  80.                 }
  81.                 return (sinFormula / graphH);
  82.         }
  83. }