x


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));
         }
     }
 }
more ▼

asked Dec 28, 2011 at 09:23 AM

Nonakesh gravatar image

Nonakesh
130 16 19 24

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Dec 28, 2011 at 10:21 PM

Kilometers gravatar image

Kilometers
151 5 10 11

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?

Dec 28, 2011 at 10:22 PM Kilometers

Thank you for that link. It is everything I needed.

Dec 31, 2011 at 02:41 AM Kilometers

Also, instead 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.

Dec 31, 2011 at 05:34 AM Kilometers

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)

Dec 28, 2011 at 10:34 PM Nonakesh
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Dec 31, 2011 at 09:20 AM

Kailas gravatar image

Kailas
61 18 12 14

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).

Jan 01, 2012 at 09:22 PM Nonakesh
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2230
x2220
x110
x1

asked: Dec 28, 2011 at 09:23 AM

Seen: 1238 times

Last Updated: Jan 01, 2012 at 09:22 PM