- Home /
Efficient wind on clouds
It seems as if my last questions were a bit too specific, but this time this is (probably) not the case!
I have a big array with clouds (planes with a cloud texture) and I want them to move with a constant velocity. This works well, but I recently bought Unity Pro and according to the profiler this cloud movement script is extremely expensive! Is there a more efficient way to do that?
function Update(){
for(var i = 0; i < cloudArray.length; i++) {
var cloud = cloudArray[i];
cloud.transform.position.x += cloudSpeed * Time.deltaTime;
if(cloud.transform.position.x > mapSize.x + cloudBorderSize) {
Destroy(cloud.gameObject);
cloudArray.RemoveAt(i);
NewCloud(-cloudBorderSize, Random.Range(-cloudBorderSize, mapSize.y + cloudBorderSize));
}
}
}
Answer by Kilometers · Dec 28, 2011 at 10:21 PM
I don't see what is so taxing about it, but you could divide the loop between two updates using a coroutine if it is that bad. Pseudopseudocode:
while (true)
{
for (first half of cloudArray)
{
...
}
yield WaitForSeconds(0.0167); // 60th of a second
for (second half of cloudArray)
{
...
}
yield WaitForSeconds(0.0167); // Another 60th of a second, makes for a clean 30fps update of the cloudArray
}
More on coroutines here: http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
ALSO ALSO ALSO! The only reason I found this post is because I cam across your earlier question about a Point-in-Polygon solution. There, you said that you found a script to handle it. Where, might I ask, did you get it?
The cloud script doesn't cause lag or something like that, but it makes huge "spikes" on the profiler! I think your script is a really good idea!
I don't know where exatly I got this script from and in fact I don't really understand it, but I made a few google searches. I used a sript similar to (maybe exactly) this one: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
If you can't get it working I can post the code tomorrow! (I'm on my laptop right now)
Also, ins$$anonymous$$d of Destroy()-ing each cloud, perhaps you could just reset its transform.position when it has traveled beyond the map. This may help with the spikes.
Answer by Kailas · Dec 31, 2011 at 09:20 AM
Replacing the destruction code with simply resetting their positions will improve efficiency since memory management is expensive.
Also, make sure that your clouds don't have colliders on them. As far as I know, having colliders (without rigidbodies on them) means that they are treated as static collision geometry. Meaning that moving them will cause the static collision system to rebuild itself every update, which is quite slow and totally negates its purpose.
Before you spend too much time on this, are the clouds inefficient enough to adversely affect gameplay? Is there a real reason to spend time improving them?
The problem is that my graphics card is very strong and I can't really tell if it affects gameplay or not, so I try to make the code as effective as possible (with the help of the profiler).