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 aaronflippo · Jul 11, 2012 at 11:13 PM · physicsiosoptimizationprofiler

Physics.Simulate taking a long time

A bit of background: My game is an "infinite runner" of sorts where the player races through an infinite world made up of very simple objects - sometimes a few hundred at a time are visible. To get an idea of what's going on with the scene, here's my latest devlog with a playable build:

http://flippfly.com/news/weekly-dev-journal-2-race-the-sun-progress/

The player uses a Character Controller. The buildings you fly past are pre-spawned from prefabs, each of them with a single box collider (one type uses a simple mesh collider,) and re-located during play from a recycled pool to a place out in front of the player.

Now, I'm having an issue where about every-other frame, Physics.Simulate is taking around 4-6 ms, on a "New Ipad." This seems extreme to me, given that the player's ship is the only object in the scene that's actually moving. It would appear on closer inspection that this time corresponds to my calls to my character controller's .move function.

One thing I have done is to disable the colliders on the buildings as I recycle them into my pool, and then just re-enable them when I place them out in front of the player. Before this, I was spiking to more like 8ms on average.

A typical scenario has around 500-600 "static colliders", 1 "active rigidbody" (which I assumed was my Player Controller? I've not placed any rigid bodies in the scene) and one Dynamic Collider (Or maybe this is my player controller?)

Additionally, I've setup the collision layers in the physics settings such that the buildings won't collide with each other and the ground - the only collisions actually allowed are player-building, and player-powerup.

Any suggestions on how to improve this situation, or profile this further? Physics.Simulate seems to be a pretty massive black box... Perhaps there's someway to tweak Unity's spacial partitioning further - or do I need to manually disable collision on objects until the player gets close to them?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Matt-Downey · Jul 12, 2012 at 01:24 AM

My best guess would be to add a co-routine that is called every 1-5 seconds that sorts through the nearest objects and either enables or disables the collider based on whether or not they are within a certain distance.

This would be a global co-routine, so the script can be separated for organization sake.

Although this is not sorting, I'm sort of referencing tonyd's post here:

http://forum.unity3d.com/threads/136097-sorting-arrays

 var thisTrans : Transform;
 var allObjects = GameObject .FindObjectsOfType (GameObject );  
 
     for (var obj in allObjects) 
         {
         var v3 : Vector3;
         var m : float; //magnitudeSquared
         v3 = obj.transform.position - thisTrans.position;
         m = v3.magnitudeSquared;
         if(m > 9000) // if the magnitude is greater than 30 root 10, or approx 95 meters
          {
          obj.collider.enabled = false;
          }
         else
          {
          obj.collider.enabled = true;
          }
         }
Comment
Add comment · Show 8 · 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 aaronflippo · Jul 12, 2012 at 12:56 PM 0
Share

Thanks $$anonymous$$att. I can probably accomplish something like this for my game without sorting, since the player only moves one direction and I know his speed. I will give that a try today.

However I'm really interested in why a scene like this can take 4-6 ms for a single character controller to move. The extents of any single movement are fairly small - the potential number of collisions should be somewhere less than 10 just based on the distribution of where the colliders are. It's almost as if the engine is considering my player's bounds to be 100 times bigger than it is, or there's something more sinister going on here with one of the colliders.

$$anonymous$$aybe physX uses a spatial partitioning that's optimized for a particular scale, and we're an order of magnitude off from that? ($$anonymous$$y player ship is something like 2 units in radius...)

$$anonymous$$ainly I'd like to know how to debug/optimize this kind of thing going forward!

avatar image Matt-Downey · Jul 15, 2012 at 02:28 AM 0
Share

For one thing, try going into the edit-->proj settings-->physics, toggle every unused checkmark in the matrix off, increase the $$anonymous$$imum collision distance to 0.05 or maybe greater, potentially turn down the solver "iteration count" from 6 or 7 to maybe 4 or 5

avatar image Matt-Downey · Jul 15, 2012 at 02:45 AM 0
Share

Before I comment any more, both me and my friend like your game over music.

I remember someone else was using triggers for ai, and the game lagged hardcore if there were a lot of spaceships (i seached for it with unity and google (custom time range and site:answers.unity3d.com) to no avail.

I think the physics system still has to do a lot of calculations using collider.bounds, to find out if anything is nearby, so just being far away isn't enough. No matter what a calculation is going to be made. If you turn off certain colliders, then you shouldn't put a large strain on the computer. What you can probably do is:

//Coroutine (once every 1-5seconds) //for(ss in spaceships) //if ss.transform.z > you.transform.z + 100 //disable collider of ss //else if ss.transform.x > you.transform.x + 100 //dito //else if ss.transform.x < you.transform.x - 100

which would run faster (maybe) since it's literally just addition subtraction and it only needs to make the first check for the majority of the calculations.

avatar image Matt-Downey · Jul 15, 2012 at 02:51 AM 0
Share

it's hard to say, but I'm doing a fps, and i also have a 2unit (2m) character, which isn't ridiculous. Just one more thing you should make sure of: are you sure it isn't the GPU or something? maybe the draw calls aren't getting batched together right?

Do you have a fixed number of obstacles at any given time? because if you do you could put together an array of Vector3 positions and Gameobjects (fixed array, and sort through which is closest using that, Unity says it themselves, you can render a million tris in about a ms using fixed arrays, the only thing is, you'd have a little extra work on your hands.

avatar image Matt-Downey · Jul 15, 2012 at 02:55 AM 0
Share

one last thing to mention, its a stretch, is your vsync enabled or disabled? edit-->proj settings-->quality-->vsync count-->you want "don't sync" (i say this because it is odd that it's VERY! stable in game around 60), can't believe i didn't mention this sooner

Show more comments

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

6 People are following this question.

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

Related Questions

Moving Colliders giving me Physics.simulate spikes in Profiler 2 Answers

Physics.simulate chews CPU but not using physics at all - Am I? 2 Answers

Profiler Physics.Simulate spikes 2 Answers

Unity deep profiling is drunk, I think 1 Answer

Unity Profiler - Physics optimization (FPS drop) 0 Answers


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