Is this Game Pause/Resume Bug?

Hello, I am newbie currently trying to make game based on Unity ECS first time.

In below video, if I right-click on my editor, then my bullets leave spaces.

Could you guys help me to check if this is only happens in scene or do I have to modify my system?

Thanks in advance!

Hard to guess without seeing the code.

Looks like your code might be behaving differently depending on the time difference between two frames (DeltaTime). When you right-click, the game stops updating, and when you close the context menu, the game simulates a frame with a large DeltaTime to make up for the passage of time. If your code isn’t written to take this into account, you might see weird behavior.

Consider the following :

  • Since it looks like you are spawning bullets periodically in some time interval (e.g. 0.1s), when the game stutters and the frame takes 1s, will your code spawn 10 bullets to compensate? Or just 1?
  • If you spawn 10, since they’re spawned on the same frame, will they be spawned in the same location?

It looks like in the above scenario you are spawning 1 bullet, and the other bullets are moved forward by a large amount to compensate for the time passage, hence the space.

Make a build of your game and try running on target hardware. Introduce some stuttering, e.g. by running some benchmark program in the background or limiting CPU frequency. See if the issue impacts the gameplay. If it works, it works.

2 Likes

My Weapon System Snippet

            foreach (var (weapon, fireMode, target, localToWorld, entity) in SystemAPI
                         .Query<RefRW<WeaponComponent>, RefRO<WeaponFireModeComponent>, RefRO<Target>,
                             RefRO<LocalToWorld>>()
                         .WithEntityAccess())
{
  if (weapon.ValueRW.timeToNextFire > 0f)
  {
      weapon.ValueRW.timeToNextFire -= Time.deltaTime;
      continue;
  }

  weapon.ValueRW.timeToNextFire = 1 / weapon.ValueRO.weaponData.Value.firesPerSecond;
  ...

My Bullet Move System Snippet

            foreach (var (bullet, transform) in SystemAPI.Query<RefRW<BulletComponent>, RefRW<LocalTransform>>())
{
    transform.ValueRW.Position += 
      transform.ValueRO.Up() * bullet.ValueRO.speed * SystemAPI.Time.DeltaTime;
}

SystemAPI.Time.DeltaTime seems to be the main cause, and the logs show a section where there is a difference of about 6 times.

Firstly, thank for your help, and how do you think about this?