- Home /
Need help to understand code!
I found this code and when i run it, the code creates particles in a 1024 by 1024 by 128 radius (Like minecraft but particles). I tried anylizing this it, but when i got about half way through i got stuck. I tried looking it up but didn;t fined anything that has a good explination of how it works. Could someone please explain what the code means that i'm confused on. If you could brake it down into what each part does would be awesome. thanks. Code: using UnityEngine; public class NoiseTest : MonoBehaviour{ private ParticleEmitter m_ParticleEmitter; // Use this for initialization private void Start() { m_ParticleEmitter = gameObject.GetComponent<ParticleEmitter>(); } private bool m_FirstTime = true; // Update is called once per frame private void Update() { Debug.Log(m_ParticleEmitter.particleCount); //number of particles in the current particel int count = 0; Particle[] particles = m_ParticleEmitter.particles; for (int x = 0; x < 1024; x++) { for (int z = 0; z < 1024; z++) { for (int y = 0; y < 128; y++) { float groundHeight = PerlinSimplexNoise.noise(x*0.001f, z*0.001f, y*0.001f)*64.0f; groundHeight += PerlinSimplexNoise.noise(x*0.01f, z*0.01f, y*0.01f)*32.0f; groundHeight += PerlinSimplexNoise.noise(x*0.1f, z*0.1f, y*0.1f)*4.0f; //groundHeight = Mathf.Clamp(groundHeight, 0, 127); particles[count].position = new Vector3(x + y, groundHeight, z); if (count < particles.Length -1) { count++; } else { UpdateDisplay(particles); return; } } } } } private void UpdateDisplay(Particle[] particles) { m_ParticleEmitter.particles = particles; } }
Answer by oxium · Mar 27, 2012 at 08:53 AM
this code is simply updating some particles position (y) until the particles array is full or it has updated all of them
then it returns from the Update function
it's using a perlin noise to move each particle height position..
not that hard to read..
anyway hope that helps..
Why does it write (x*0.1f, z*0.1f, y*0.1f)*4.0f ins$$anonymous$$d of (x*0.1, z*0.1, y*0.1)*4.0 without the "f"? Is there a reason it should use f? will it not work without it?
the f after a number is a quick way to indicate a float. It is not required if the code already knows the number is a float or if you're using Javascript but it is a good practise to use it.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Rotating GameObject with transform.Rotate issue! 1 Answer
Implementing wander mode 0 Answers
this script in Javascript?? 0 Answers
Unity mathematics phrasing. 1 Answer