Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by manny003 · Feb 24, 2015 at 04:32 PM · mobileperformancerigidbody2dfixedupdate

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Louis-N-D · Feb 24, 2015 at 07:25 PM

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[i].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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image manny003 · Feb 24, 2015 at 09:09 PM 0
Share

Holly hot damn.. that's it. Simple enough where I don't have restructure too much.

Of course, with enough time, I probably would have come up with the same thing. ;)

Thanks much.

UPDATE: I also realized, in testing my code, that I needed not worry about accounting for the missing smoothDeltaTime when "skipping" frames. I simply only needed to double my move speed when only moving in half the frames... duh!

avatar image Louis-N-D · Feb 24, 2015 at 09:29 PM 0
Share

And if you wanted to go with the configurable stepsNumber idea, you can always multiply your move speed by the stepsNumber.

avatar image manny003 · Feb 24, 2015 at 09:42 PM 0
Share

Very true.. however, now that the code is optimized -- each frame runs quicker so its not an exact 1:1 increase on speed multiplier to framestep number -- as I just found out.

But it's an issue I'll gladly take as now my code is running better than ever.

avatar image
0

Answer by maccabbe · Feb 24, 2015 at 09:44 PM

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
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image manny003 · Feb 24, 2015 at 09:55 PM 0
Share

Also another excellent idea. Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Does using bouncy physical material lower performance on mobile devices drastically? 1 Answer

Levels: scenes or GameObjects? 3 Answers

Number of vertices for android 0 Answers

How to improve device heating (Android) 1 Answer

Batching on Mobile = Bad? Your experience. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges