Search Unity

Moving collider duruing Time.timeScale == 0

Discussion in 'Scripting' started by Giustitia, Jan 22, 2020.

  1. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    Hi everybody!

    So, I guess this is a common question but I couldn't find an answer over the net. So, I'm trying to move an object while timeScale == 0. To do that, I'm raycasting from screen position against a collision layer, grab an object if found, move it with the mouse and then left click to release it. The problem is that, since timeScale == 0, the collider of the object (I guess) is not updating its position. I notice that because if I want to grab again the object that I moved, I have to click on the position where it was before the movement and not on the new position it actually is.

    Any solution? :D
     
    ciubix8514 and D1QK6kk like this.
  2. Giustitia

    Giustitia

    Joined:
    Oct 26, 2015
    Posts:
    113
    So, as it use to happens, I've found the problem by myself :D

    Just go to the Physic tab in Project Setting and check "Auto Sync Transforms", or, even better, do it manually when needed using Physics.SyncTransforms.

    https://docs.unity3d.com/ScriptReference/Physics-autoSyncTransforms.html

    Thanks anyways, hope I helped someone with this fast but intense thread, now or over the next 100 years :D
     
  3. Higgs_Field

    Higgs_Field

    Joined:
    Jun 10, 2020
    Posts:
    1
    Thank you for posting your solution!
     
    RadaRadar and Giustitia like this.
  4. KYL3R

    KYL3R

    Joined:
    Nov 16, 2012
    Posts:
    135
    Happened to me in my Pause Screen, where I set Timescale to 0f. I can still animate stuff using Time.unscaledDeltaTime etc. but the OnMouseEnter was off, because the collider didn't move when paused. (menu elements fade in horizontally)

    I could see this in the Physic Debugger, which showed the UI was moving, but the collider was stuck (not synced).

    toggling the "enabled" state of the BoxCollider off and on helped, but I feared I might trigger the OnMouseEnter every frame by doing so.

    Syncing the Physics is exactly what I needed while in the pause menu. Note that you have to call this in Update, as FixedUpdate isn't called when timescale is 0.
     
    Last edited: Apr 22, 2022
  5. Black_six

    Black_six

    Joined:
    Dec 8, 2020
    Posts:
    11
    Thank you!This is a good answer!It save my ass.