Need Advice: How to alternate FixedUpdates for GameObjects

I have several hundred gameObjects which have RigidBody2D and Colliders2D attached. They are all Kinemmatics gameObjects and I am currently moving them with code in FixedUpdate.

But I don’t necessary want or need all the game objects moved every frame. They are relatively slow moving enough that I would be ok moving them every-other-frame instead. What I would like to do is move half the game objects in one physics frame and the other half in the next physics frame, back and fourth and so on.

Changing the time step is not really an option for me here since I also have other much faster moving rigid bodies as well.

My code to move them is working fine and all the collision detection is working. My reason for asking is to try to squeeze out every ounce of performance I can for mobile.

Can anyone suggest a recipe for this? I am coding in Unity Script/Javascript.

Also, in my move calculations, I am using Time.smoothDeltaTime so still need that to reflect “time since last frame” to include the frames skipped for gameObjects.

I am developing for iOS devices and perhaps Android down the line.

Thanks in advance.
Manny

Hm… Not sure of a SPECIFIC implementation, but seems like a possible first step would be to… maybe number them somehow. So, say you add a movement ID to your movement script for each object, then only move the object if its ID is odd or even to match the current frame?

The objects could even be numbered automatically at start up. Give them all the same tag and have a script that runs a findObjectsWithTag to grab an array of all of them, you can then assign their ID based on their index in the array.

Here’s another idea in pseudocode if you’ll indulge me.

In the initialization script:

public var stepsNumber : int = 2;
    
    function Start()
    {
       var tempArray = FindObjectsWithTag("MyTag");
       for(var i : int = 0; i < tempArray.Length ; i++)
       {
          do what you need to do to get access to the moveID...
          object*.moveID = i % stepsNumber;*

}
}
Here, stepsNumber is the number of move steps you want… So, to alternate between moving each object every second frame? stepsNumber = 2. If you have even more objects and you want to cycle between three groups every three frames then stepsNumber = 3.
In the object’s movement script:
public var moveID : int; //this is the id that gets checked for whether or not to move
private var currentTickStep : int; //this is incremented at every FixedUpdate
function Start()
{
stepsNumber = GetComponent… blah blah blah get a reference from the init script.
}
function FixedUpdate()
{
if((currentTickStep % stepsNumber) = moveID)
{
Do your movement thing!
}
currentTickStep++;
}
So, the idea here is that by using a modulo (%) of the currentTickStep and your stepsNumber you will get a result that just keeps looping from 1 (or is it 0) to the stepsNumber. Only when this result matches the object’s moveID to you actually move the object.

If you want to do fixed update for half then the other half it might be worth using a class to control the updates. The main benefit would be to let you balance the number of objects that are updated each step. However it might require a bit of restructing.

using UnityEngine;
using System.Collections.Generic;

public class Controller : MonoBehaviour{
    List<Alternator> list1=new List<Alternator>();
    List<Alternator> list2=new List<Alternator>();
    int step;
    public void FixedUpdate(){
        step=(step+1)%2;
        if(step==1) {
            foreach(Alternator script in list1) {
                script.AlternateUpdate();
            }
        }
        else {
            foreach(Alternator script in list2) {
                script.AlternateUpdate();
            }        
        }
    }
}

interface Alternator{
    void AlternateUpdate();
}

public class Object : MonoBehaviour, Alternator {
    public void AlternateUpdate() { 
        // stuff goes here
    }
}