- Home /
How to detect collision when Time.timeScale == 0?
So I have my Time scale set to 0 while paused in my bullet hell game. When un-pausing I want to force the player to return the mouse to their previous position so they can't just teleport via pausing. I'm doing this by creating a "ghost" at the player position (player follows mouse btw), then when the player unpauses, the timescale stays one until the player returns to the ghost position.
The problem is that at timescale 0 no collisions are detected! I have this attached to the ghost object
private void OnTriggerEnter2D(Collider2D collision)
{
print("collision");
if (collision.gameObject.tag.Equals("Player"))
{
Time.timeScale = 1f;
Destroy(gameObject);
PauseMenu.Paused = false;
}
}
So yeah. How do I detect collisions when timescale = 0?
Answer by Ermiq · Dec 09, 2019 at 11:47 AM
As far as I know, you can't do that via collision detection.
Instead, you can use raycasting to compare the ghost position and the mouse position (raycasting works with timescale 0). For example:
RaycastHit hitInfo;
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hitInfo))
{
if (hitInfo.gameObject.tag.Equals("Ghost"))
{
Time.timeScale = 1f;
Destroy(hitInfo.gameObject);
PauseMenu.Paused = false;
}
}
Thank you! I forgot to mention that Im working in 2D, but this definitely sent me down the right path. It's all working now!
While this certainly works in a lot of cases, OnTiggerEnter2D is not a RayCast.
Answer by Mockarutan · Dec 11, 2019 at 01:42 PM
You can drive the simulation yourself explained here: https://docs.unity3d.com/ScriptReference/Physics.Simulate.html