Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Check OnTrigger collisions then timeScale is set to zero

Discussion in 'Physics' started by MayoNinjaGames, Jul 11, 2019.

  1. MayoNinjaGames

    MayoNinjaGames

    Joined:
    Nov 7, 2018
    Posts:
    31
    So I have a menu for placing traps and I want the game to be paused while the player is trying to place a trap. I need collision checking to make sure the player isn't placing it on solid objects. The only issue I have left is that the physics system isn't running when timeScale is set to zero. I know this could be solved by using raycasts, but I would like to avoid using them since I would have to rearrange all my layers. Only other better solution I've come up with is to set the the timeScale to 0.25f when on mouseUp and then set it to 0 again next frame, but that feels weird.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
  3. MayoNinjaGames

    MayoNinjaGames

    Joined:
    Nov 7, 2018
    Posts:
    31
    Code (CSharp):
    1. // Update is called once per frame
    2.     void Update()
    3.     {
    4.  
    5.         if (Input.GetMouseButton(0) && Input.mousePosition.y > 100) {
    6.             transform.position = cam.ScreenToWorldPoint(Input.mousePosition);
    7.             transform.position = new Vector3(transform.position.x, transform.position.y, 0);
    8.  
    9.         }
    10.  
    11.         timer += Time.unscaledDeltaTime;
    12.  
    13.         while (timer >= Time.fixedDeltaTime) {
    14.             timer -= Time.fixedDeltaTime;
    15.             Physics2D.Simulate(Time.fixedDeltaTime);
    16.         }
    17.  
    18.     }
    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collision) {
    2.         colliding = true;
    3.  
    4.         print("Colliding");
    5.     }
    I've tried this, but OnTriggerEnter or Exit is still not being called. Did I mess up the variables? I chose unscaledDeltaTime since timeScale is zero.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,314
    Note that It's much easier to just run it from "FixedUpdate" and not trying to emulate fixed-update from "Update" just calling the simulate with the fixed-deltat-time.

    I'm not sure what the "transform.position" stuff is all about but you should never, ever move GameObject that have physics objects on them using transform as you're just teleporting them from position to position. Use "Rigidbody2D.MovePosition" to do that.

    Honestly, it's hard to know as the code above only shows changing a transform, running the simulation emulating fixed-update and a callback.

    Your original problem was detecting if things were below a player and by far the best way to do that is using physics queries and probably not using raycast as you suggested. There are two main queries that will check for overlap i.e.OverlapPoint and OverlapCollider both of which can be called via a collider or a Rigidbody2D which will use all attached colliders on the rigidbody to check for overlap. This would be the best way to do this and doesn't require simulation or physics interaction (col-det).
     
  5. andreim44

    andreim44

    Joined:
    Apr 17, 2014
    Posts:
    13
    Hi there, I just wanted to point what may be an error in your answer or a bug.
    I was not aware of this, but it seems that FixedUpdate is NOT called at all while Timescale is set to 0. - so the solution above trying to emulate FixedUpdate from update and call Physics from there is the only that worked for me.
    I'm not sure if FixedUpdate not being called is a feature or a bug, but in Unity 2020.3.6f1 I can confirm it is not called at all with timescale 0.