- Home /
How can I change WindZone settings with a script?
I've been having a lot of fun experimenting with WindZones and trees I've made in Unity's Tree Creator. It's amazing the amount of realism you can get with just the right settings.
What I'd like to do next is make the settings in a WindZone in my scene change via a script. I'm using UniSky and want my WindZone settings to change based on the different weather. Stormy?...trees are whipping in the wind. Clear?...trees are completely still.
I looked in the scripting reference and reference manual but couldn't find anything that explains how to do this. Is it possible?
Seems this bug still exists. Pretty poor, since Unity themselves thought it was available right back in 3.0.0.
http://forum.unity3d.com/threads/60593-Access-Terrain-Wind-Setting-and-Wind-Zone-in-Unity3D-3.0.02f
Answer by knowpixels · Feb 25, 2015 at 02:21 PM
You dont need to hack around with reflections if you understand Unity typing paradigms.
The WindZone DataType is private in Unity as of 3.x - this is still true as of 4.5.x This means you cannot access its properties using strict typing (#pragma strict) and typeof(). This is because WindZone is not strictly a type (public data type), so if you try GetComponent(WindZone) or GetComponent(typeof(WindZone)) you will quite rightly receive an unknown data type error message from Unity Console. All you have to do is switch to dynamic typing, it is this easy in JS/US:
Remove #pragma strict from the top of your script.
Change any usage instance of GetComponent(WindZone) or GetComponent(typeof(WindZone)) to GetComponent("WindZone"); (Include speech marks!) and remove any variable type declarations from WindZone usages.
So instead of:
#pragma strict
var wz:WindZone = GetComponent(WindZone);
Your script should look like:
//#pragma strict
var wz = GetComponent("WindZone");
Now the unknown type error in Unity console will be gone and your script has access to WindZone script properties through the var called "wz" you can now do:
wz.windMain = 1;
wz.windTurbulence = 1.2;
wz.windPulseMagnitude= 0.6;
wz.windPulseFrequency= 0.2;
Now you have full run time visibility and control of your in game blustery goodness.
Answer by Waz · Aug 18, 2011 at 05:12 PM
One option that I have used (in a WebPlayer where reflection hacks are forbidden) is to have an animation that changes the values. You can then play (or more likely, Sample, though I didn't need that much precision in my case) the animation to change the values.
Answer by StefanoCecere · Jul 24, 2012 at 04:38 AM
i too was looking for Wind Zone class/references... there aren't yet! :(
Answer by getyour411 · Apr 24, 2014 at 11:02 PM
One change you can do, assuming WindZone is marked as Direction, is access the WindZone's transform via script and rotate it. I tested that by adding a simple JS that I got somewhere to my WindZone's gameobject and watched my WindZone rotate (I only did Y) and my TerrainTrees react to that change realtime.
var rotationSpeedX:float=0;
var rotationSpeedY:float=5;
var rotationSpeedZ:float=0;
var local:boolean=true;
private var rotationVector:Vector3=Vector3(rotationSpeedX,rotationSpeedY,rotationSpeedZ);
function Update () {
if (local==true) transform.Rotate(Vector3(rotationSpeedX,rotationSpeedY,rotationSpeedZ)*Time.deltaTime);
if (local==false) transform.Rotate(Vector3(rotationSpeedX,rotationSpeedY,rotationSpeedZ)*Time.deltaTime, Space.World);
}