best way to move a big amount (hundreds) of transforms.

A bit of background. I have a script that instances on start my desired number of gameobjects named grassplane. Grassplane is a quad with a texture for grass. The game is an endless sidescroller.

The result in itself is pretty good, visually speaking. I even created my own pooling system so they go back to the spawnpoint without any error. Well, it’s not even really pooling as in storing them anywhere, they just cycle endlessly instead of instance-destroy.

My problem comes with how much computing they need to move endlessly.

Each grassplane has the following code:

function Start () {

function Update () {
    speed = speed_logic.speed;
    transform.Translate(-Vector3.right * speed * dir * Time.deltaTime);

	    if (posx<bg_trees.endpoint){
	  		tr.position = Vector3((posx-bg_trees.endpoint)+bg_trees.startpoint, posy, posz);
	  		//transform.position = Vector3((posx-bg_trees.endpoint)+bg_trees.startpoint, posy, posz);

The whole tr=transform comes from a certain tutorial that mentions that everytime i’m simply saying “transform” it uses getcomponent, which is an expensive call, but it feels like they result in the same performance actually.

I tried grouping many of the grassplanes so instead of calling 500 individual planes, I’d be calling 100 to a parent gameobject of 5 grassplanes, but from the profiler I draw the conclusion that it’s the same cost performance-wise.

Is there any way to make it less expensive?

Thanks in advance,

Hi blitzcloud

There isn’t any simle way to move multiple objects at once without incurring a fairly high cost. However I would suggest a few options that might help:

  • Firstly, you are still reading ‘transform’ several places in that code (twice in your update function). This is an expensive operation and it will help performance if you remove it.
  • The ‘if’ statement is an extra check that you can probably minimize. Maybe just have 1 bit of code that checks once per second, or if you can calculate how long it’ll be before the grass goes off screen, have a coroutine that sleeps until its time to respawn
  • It might be worth trying putting the grass in a none-collidable rigid body, then just set its speed. If all grass moves at the same rate this’ll be much faster, as unity does the moving for you. This would be most effective providing you can eliminate the ‘Update’
  • Another trick might be to render the grass to a different camera, then just move the camera instead!

If it we me, my general approach if I wanted ‘lots of grass’ would be:

  • Have a few big objects that contain lots of grass that moves at the same speed (maybe 100 bits each)
  • Move those objects using rigid bodies so unity does the job
  • Respawn the big objects one at a time - as you’ve only got 5 objects going, instead of 500 this is fine
  • If you can, perhaps get an artist to simple make big meshes that contain all your grass!

In general, reduce number of objects is the way to go!