Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Mesh Collider Doesn't get scaled in same frame consistently.

Discussion in 'Physics' started by Fedora_Dev, May 12, 2020.

  1. Fedora_Dev

    Fedora_Dev

    Joined:
    Oct 28, 2016
    Posts:
    35
    Hello

    I am trying to write a bit of code that will essentially do a physics overlap check with any mesh. I don't plan on doing it every frame, nor will I be using complex meshes to do so (max 17 verts / 12 tris). The shape is convex. I've got the code running finally. The only issue I'm having now is if i set the scale of the collider's transform before checking the cast, the new scale is ignored and it instead uses the scale from just before it was changed. I thought maybe the scale of the collider wasn't updated until the next frame, so I put a delay of up to 3 frames after scaling to check, however sometimes it updates properly and sometimes it doesn't. I'm not sure why it has decided to scale whenever it wants, and I can't find anything in the docs about the issue.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    8,884
    Physics by default doesn't run per-frame, it runs during the fixed-update which isn't related to frames. You could have 10 frames rendered between physics updating depending on the fixed frequency set.

    Anyway, modifying a Transform doesn't immediately change physics components as they don't know anything about the change. Physics(2D).SyncTransforms is done once prior to the simulation running which gathers changes and performs updates. I would not recommend you calling this explicitly lots of times as it has a cost associated with it but it is a public method.

    The Physics(2D).AutoSyncTransforms is off by default for this reason, it's expensive. With this on, anytime you perform a read operation, the above transform sync happens so before every physics query or reading a rigidbody pose etc. I would not recommend this be on.

    If you must scale like this then try not to do it per-frame and make all the scale changes first then wait for the simulation to run either through the FixedUpdate or manually yourself. If you must do it per-frame then do the same and either run the simulation per-frame or call SyncTransforms above once only. Still, be careful with performance there. If you make a change, call sync-transforms, make another change, call sync-transforms then be prepared for poor performance.
     
    Fedora_Dev likes this.
  3. Fedora_Dev

    Fedora_Dev

    Joined:
    Oct 28, 2016
    Posts:
    35
    Ah yes, waiting for a fixed update seems to have fixed the issue. Not sure why I didn't try that yet.

    And yes, I'm aware of the potential performance issues. I doubt the player would be able to force this to happen more than once every 5 seconds if they're booking it, and I'm trying to keep the poly count on these casting objects extremely low. Thanks for mentioning it, though, and for going into detail.