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.
Just run the physics manually then you have full control.
// Update is called once per frame
void Update()
{
if (Input.GetMouseButton(0) && Input.mousePosition.y > 100) {
transform.position = cam.ScreenToWorldPoint(Input.mousePosition);
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
}
timer += Time.unscaledDeltaTime;
while (timer >= Time.fixedDeltaTime) {
timer -= Time.fixedDeltaTime;
Physics2D.Simulate(Time.fixedDeltaTime);
}
}
private void OnTriggerEnter2D(Collider2D collision) {
colliding = true;
print("Colliding");
}
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.
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).
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.