Search Unity

Question about Physics.autoSyncTransforms and Raycast

Discussion in 'Physics' started by hexdump, Aug 15, 2019.

  1. hexdump

    hexdump

    Joined:
    Dec 29, 2008
    Posts:
    439
    Hi,

    I always have been using raycasts in update. Doesn't make sense to use them in fixedUpdate. First becasue for my 2D games I need tight controls/collisions and because fixedUpdate can be executed a bunch of times per physics frame to catch up with rendering.

    So, now that physics.autoSyncTransforms is set to false by default, does this mean I wont be able to use raycast in a effective way in Update? I mean, with physics.autoSyncTransforms=true as I move a gameobject through its transform, the physical word is synced immediately. So, I can do raycast query and get correct results based on this frame previous transformations . Now, with physics.autoSyncTransform=false (default) will I be raycasting against an outdated physical world until next fixedStep is executed?

    Cheers.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,968
    You should never modify a Transform that has a physics component on it, period. If you want to modify the position of something, use the Rigidbody via MovePosition/Velocity/Force.

    I presume by your description you're modifying the position of "something" each frame which might be several times between actual physics updates. Why are you doing that exactly?

    If you're happy to run a variable time-step simulation in your project then you can perform a manual simulation per-frame then you don't have to worry about a discontinuity between physics updates and render updates. You can also ignore things like interpolation etc.

    If you absolutely must explicitly position a body each frame then you can set its position directly using Rigidbody2D.position/rotation. Note that this causes its contacts to be destroyed (pending a simulation update) and stops interpolation until the next simulation update.
     
  3. hexdump

    hexdump

    Joined:
    Dec 29, 2008
    Posts:
    439
    @MelvMay I forgot to say by rigidbody was a kinematic one. So, this afirmation isn't right: "You should never modify a Transform that has a physics component on it, period.". Isn't it?

    So, this is why I am asking physics.autoSyncTransforms thing.

    Thanks ina dvance.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,968
    You should never modify the transform no matter what the body-type is, Modifying the transform is the responsiblity of a kinematic or dynamic body. Go via the body itself to get the transform changed.
     
    hexdump likes this.