Search Unity

Bug Bullet spawn system gives NaN for Translation / Rotation with 0.3.0 (0.2.5 works)

Discussion in 'Physics for ECS' started by schaefsky, Mar 15, 2020.

  1. schaefsky

    schaefsky

    Joined:
    Aug 11, 2013
    Posts:
    90
    Upgrade to 0.3.0 broke my bullet spawn system:


    Downgrade to 0.2.5 preview.1 and it works again.

    Spawnsystem:
    Code (CSharp):
    1. using Unity.Collections;
    2. using Unity.Entities;
    3. using Unity.Jobs;
    4. using Unity.Transforms;
    5. using Unity.Physics;
    6.  
    7. [UpdateInGroup(typeof(SimulationSystemGroup))]
    8. public class BulletSpawnerJobSystem : JobComponentSystem
    9. {
    10.     private BeginSimulationEntityCommandBufferSystem m_EntityCommandBufferSystem;
    11.  
    12.     protected override void OnCreate()
    13.     {
    14.         m_EntityCommandBufferSystem = World.GetOrCreateSystem<BeginSimulationEntityCommandBufferSystem>();
    15.     }
    16.  
    17.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    18.     {
    19.         var bulletSpawnerInfo = GetSingleton<BulletSpawnerInfo>();
    20.         var ecb = m_EntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
    21.  
    22.         var jobHandle = Entities.WithAll<ShootingComponent>().ForEach((Entity entity, int nativeThreadIndex, in LocalToWorld ltw) =>
    23.         {
    24.             var instance = ecb.Instantiate(nativeThreadIndex, bulletSpawnerInfo.Prefab);
    25.  
    26.             ecb.SetComponent(nativeThreadIndex, instance, new Rotation { Value = ltw.Rotation.value });
    27.             ecb.SetComponent(nativeThreadIndex, instance, new Translation { Value = ltw.Position + ltw.Forward * 5f });
    28.             ecb.SetComponent(nativeThreadIndex, instance, new PhysicsVelocity { Linear = bulletSpawnerInfo.Speed * ltw.Forward });
    29.             ecb.AddComponent(nativeThreadIndex, instance, new TimeToLive { TTL = bulletSpawnerInfo.TTL });
    30.             ecb.AddComponent(nativeThreadIndex, instance, new BulletComponent { damage = bulletSpawnerInfo.Damage });
    31.  
    32.             ecb.RemoveComponent<ShootingComponent>(nativeThreadIndex, entity);
    33.             ecb.AddComponent(nativeThreadIndex, entity, new CoolDown { Value = 0.2f });
    34.         }).Schedule(inputDeps);
    35.  
    36.         m_EntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);
    37.         return jobHandle;
    38.     }
    39. }
     
    nanobot_games and NotaNaN like this.
  2. Adam-Mechtley

    Adam-Mechtley

    Administrator

    Joined:
    Feb 5, 2007
    Posts:
    290
    What versions of entities and Burst were you running before the upgrade? Can you share your manifest.json file before/after the update?
     
    schaefsky likes this.
  3. schaefsky

    schaefsky

    Joined:
    Aug 11, 2013
    Posts:
    90
    Before the update I was running Entities 0.6.0 (or 0.7.0, sorry I am not sure), Physics 0.2.5 preview 1.

    I did an update with current project Entities 0.8.0 physics 0.2.5 preview 1 whcih works:

    Code (CSharp):
    1. {
    2.   "dependencies": {
    3.     "com.unity.2d.sprite": "1.0.0",
    4.     "com.unity.2d.tilemap": "1.0.0",
    5.     "com.unity.ads": "2.0.8",
    6.     "com.unity.analytics": "3.3.5",
    7.     "com.unity.burst": "1.3.0-preview.6",
    8.     "com.unity.collab-proxy": "1.2.16",
    9.     "com.unity.entities": "0.8.0-preview.8",
    10.     "com.unity.ext.nunit": "1.0.0",
    11.     "com.unity.ide.rider": "1.1.4",
    12.     "com.unity.ide.vscode": "1.1.4",
    13.     "com.unity.inputsystem": "1.0.0-preview.6",
    14.     "com.unity.jobs": "0.2.7-preview.11",
    15.     "com.unity.mathematics": "1.1.0",
    16.     "com.unity.physics": "0.2.5-preview.1",
    17.     "com.unity.purchasing": "2.0.6",
    18.     "com.unity.rendering.hybrid": "0.4.0-preview.8",
    19.     "com.unity.test-framework": "1.1.11",
    20.     "com.unity.textmeshpro": "2.0.1",
    21.     "com.unity.timeline": "1.2.13",
    22.     "com.unity.ugui": "1.0.0",
    23.     "com.unity.modules.ai": "1.0.0",
    24.     "com.unity.modules.androidjni": "1.0.0",
    25.     "com.unity.modules.animation": "1.0.0",
    26.     "com.unity.modules.assetbundle": "1.0.0",
    27.     "com.unity.modules.audio": "1.0.0",
    28.     "com.unity.modules.cloth": "1.0.0",
    29.     "com.unity.modules.director": "1.0.0",
    30.     "com.unity.modules.imageconversion": "1.0.0",
    31.     "com.unity.modules.imgui": "1.0.0",
    32.     "com.unity.modules.jsonserialize": "1.0.0",
    33.     "com.unity.modules.particlesystem": "1.0.0",
    34.     "com.unity.modules.physics": "1.0.0",
    35.     "com.unity.modules.physics2d": "1.0.0",
    36.     "com.unity.modules.screencapture": "1.0.0",
    37.     "com.unity.modules.terrain": "1.0.0",
    38.     "com.unity.modules.terrainphysics": "1.0.0",
    39.     "com.unity.modules.tilemap": "1.0.0",
    40.     "com.unity.modules.ui": "1.0.0",
    41.     "com.unity.modules.uielements": "1.0.0",
    42.     "com.unity.modules.umbra": "1.0.0",
    43.     "com.unity.modules.unityanalytics": "1.0.0",
    44.     "com.unity.modules.unitywebrequest": "1.0.0",
    45.     "com.unity.modules.unitywebrequestassetbundle": "1.0.0",
    46.     "com.unity.modules.unitywebrequestaudio": "1.0.0",
    47.     "com.unity.modules.unitywebrequesttexture": "1.0.0",
    48.     "com.unity.modules.unitywebrequestwww": "1.0.0",
    49.     "com.unity.modules.vehicles": "1.0.0",
    50.     "com.unity.modules.video": "1.0.0",
    51.     "com.unity.modules.vr": "1.0.0",
    52.     "com.unity.modules.wind": "1.0.0",
    53.     "com.unity.modules.xr": "1.0.0"
    54.   }
    55. }
    Update to physics 0.3.1 (and Burst 1.3.0 preview 8, same issue before and after Burst update, phyics update seems to cause it):
    Code (CSharp):
    1. {
    2.   "dependencies": {
    3.     "com.unity.2d.sprite": "1.0.0",
    4.     "com.unity.2d.tilemap": "1.0.0",
    5.     "com.unity.ads": "2.0.8",
    6.     "com.unity.analytics": "3.3.5",
    7.     "com.unity.burst": "1.3.0-preview.8",
    8.     "com.unity.collab-proxy": "1.2.16",
    9.     "com.unity.entities": "0.8.0-preview.8",
    10.     "com.unity.ext.nunit": "1.0.0",
    11.     "com.unity.ide.rider": "1.1.4",
    12.     "com.unity.ide.vscode": "1.1.4",
    13.     "com.unity.inputsystem": "1.0.0-preview.6",
    14.     "com.unity.jobs": "0.2.7-preview.11",
    15.     "com.unity.mathematics": "1.1.0",
    16.     "com.unity.physics": "0.3.1-preview",
    17.     "com.unity.purchasing": "2.0.6",
    18.     "com.unity.rendering.hybrid": "0.4.0-preview.8",
    19.     "com.unity.test-framework": "1.1.11",
    20.     "com.unity.textmeshpro": "2.0.1",
    21.     "com.unity.timeline": "1.2.13",
    22.     "com.unity.ugui": "1.0.0",
    23.     "com.unity.modules.ai": "1.0.0",
    24.     "com.unity.modules.androidjni": "1.0.0",
    25.     "com.unity.modules.animation": "1.0.0",
    26.     "com.unity.modules.assetbundle": "1.0.0",
    27.     "com.unity.modules.audio": "1.0.0",
    28.     "com.unity.modules.cloth": "1.0.0",
    29.     "com.unity.modules.director": "1.0.0",
    30.     "com.unity.modules.imageconversion": "1.0.0",
    31.     "com.unity.modules.imgui": "1.0.0",
    32.     "com.unity.modules.jsonserialize": "1.0.0",
    33.     "com.unity.modules.particlesystem": "1.0.0",
    34.     "com.unity.modules.physics": "1.0.0",
    35.     "com.unity.modules.physics2d": "1.0.0",
    36.     "com.unity.modules.screencapture": "1.0.0",
    37.     "com.unity.modules.terrain": "1.0.0",
    38.     "com.unity.modules.terrainphysics": "1.0.0",
    39.     "com.unity.modules.tilemap": "1.0.0",
    40.     "com.unity.modules.ui": "1.0.0",
    41.     "com.unity.modules.uielements": "1.0.0",
    42.     "com.unity.modules.umbra": "1.0.0",
    43.     "com.unity.modules.unityanalytics": "1.0.0",
    44.     "com.unity.modules.unitywebrequest": "1.0.0",
    45.     "com.unity.modules.unitywebrequestassetbundle": "1.0.0",
    46.     "com.unity.modules.unitywebrequestaudio": "1.0.0",
    47.     "com.unity.modules.unitywebrequesttexture": "1.0.0",
    48.     "com.unity.modules.unitywebrequestwww": "1.0.0",
    49.     "com.unity.modules.vehicles": "1.0.0",
    50.     "com.unity.modules.video": "1.0.0",
    51.     "com.unity.modules.vr": "1.0.0",
    52.     "com.unity.modules.wind": "1.0.0",
    53.     "com.unity.modules.xr": "1.0.0"
    54.   }
    55. }
    56.  
    Reported issue is present when Physics upgraded to higher than 0.2.5 preview 1, after downgrade to Physics 0.2.5 preview 1 it works again.
     
  4. nanobot_games

    nanobot_games

    Joined:
    Jul 22, 2019
    Posts:
    10
    I am having same issue, I had this issue on both 2019.3.51f and 2020.1.0b3 and different versions of entities, burst, collections, and rendering. The issue is when I upgrade Physics beyond 0.2.5 preview, with both Physics 0.3.0 and 0.3.1. Also I get the same issue with Havok Physics if I upgrade beyond 0.1.2.

    Initially I realised I was having an issue as the fps was incrementally dropping from when I hit play. In the profiler all I could identify was semaphore.wait for signal.waitforsignal and gfx.waitforpresent which was affecting my the ecs jobs which read from transforms.

    After reading schaefsky's post, I realised with the updated physics I also get NaN transforms with dynamic rigid bodies. Not sure if the really low fps is an effect of having the NaNs or related to the cause. Also, I'm using convert to entity workflow. Attached is my manifest.json before updating (I have tried both unity physics 0.2.5 and havok physics 0.1.2 and they are fine but not after upgrade):

    Code (JavaScript):
    1.  
    2. {
    3.   "dependencies": {
    4.     "com.havok.physics": "0.1.2-preview",
    5.     "com.unity.burst": "1.3.0-preview.8",
    6.     "com.unity.collab-proxy": "1.3.7",
    7.     "com.unity.collections": "0.7.0-preview.2",
    8.     "com.unity.dots.editor": "0.4.0-preview",
    9.     "com.unity.entities": "0.8.0-preview.8",
    10.     "com.unity.ide.rider": "1.2.1",
    11.     "com.unity.ide.visualstudio": "2.0.0",
    12.     "com.unity.ide.vscode": "1.1.4",
    13.     "com.unity.jobs": "0.2.7-preview.11",
    14.     "com.unity.mathematics": "1.1.0",
    15.     "com.unity.quicksearch": "1.5.2",
    16.     "com.unity.recorder": "2.1.0-preview.1",
    17.     "com.unity.render-pipelines.lightweight": "8.0.1",
    18.     "com.unity.rendering.hybrid": "0.4.0-preview.8",
    19.     "com.unity.shadergraph": "8.0.1",
    20.     "com.unity.test-framework": "1.1.11",
    21.     "com.unity.textmeshpro": "3.0.0-preview.1",
    22.     "com.unity.timeline": "1.2.10",
    23.     "com.unity.ugui": "1.0.0",
    24.     "com.unity.visualeffectgraph": "8.0.1",
    25.     "com.unity.modules.ai": "1.0.0",
    26.     "com.unity.modules.androidjni": "1.0.0",
    27.     "com.unity.modules.animation": "1.0.0",
    28.     "com.unity.modules.assetbundle": "1.0.0",
    29.     "com.unity.modules.audio": "1.0.0",
    30.     "com.unity.modules.cloth": "1.0.0",
    31.     "com.unity.modules.director": "1.0.0",
    32.     "com.unity.modules.imageconversion": "1.0.0",
    33.     "com.unity.modules.imgui": "1.0.0",
    34.     "com.unity.modules.jsonserialize": "1.0.0",
    35.     "com.unity.modules.particlesystem": "1.0.0",
    36.     "com.unity.modules.physics": "1.0.0",
    37.     "com.unity.modules.physics2d": "1.0.0",
    38.     "com.unity.modules.screencapture": "1.0.0",
    39.     "com.unity.modules.terrain": "1.0.0",
    40.     "com.unity.modules.terrainphysics": "1.0.0",
    41.     "com.unity.modules.tilemap": "1.0.0",
    42.     "com.unity.modules.ui": "1.0.0",
    43.     "com.unity.modules.uielements": "1.0.0",
    44.     "com.unity.modules.umbra": "1.0.0",
    45.     "com.unity.modules.unityanalytics": "1.0.0",
    46.     "com.unity.modules.unitywebrequest": "1.0.0",
    47.     "com.unity.modules.unitywebrequestassetbundle": "1.0.0",
    48.     "com.unity.modules.unitywebrequestaudio": "1.0.0",
    49.     "com.unity.modules.unitywebrequesttexture": "1.0.0",
    50.     "com.unity.modules.unitywebrequestwww": "1.0.0",
    51.     "com.unity.modules.vehicles": "1.0.0",
    52.     "com.unity.modules.video": "1.0.0",
    53.     "com.unity.modules.vr": "1.0.0",
    54.     "com.unity.modules.wind": "1.0.0",
    55.     "com.unity.modules.xr": "1.0.0"
    56.   }
    57. }
     
    schaefsky likes this.
  5. MartinTilo

    MartinTilo

    Unity Technologies

    Joined:
    Aug 16, 2017
    Posts:
    2,461
    Sounds like vSync, targetFrameRate or a GPU bottleneck is holding you back, depending on what sample that Semaphore appears under and how the timings align with the ones on the Render thread. Please consult the Profiler documentation regarding these samples.
     
  6. nanobot_games

    nanobot_games

    Joined:
    Jul 22, 2019
    Posts:
    10
    Thanks for replying! I looked it up when I was having the issue and tried vsync on and off and gpu profiler but nothing jumped out. Maybe I misinterpreted the profiler as I dont see how a gpu bottleneck could cause it to go from 65fps to 4fps on updating the physics package.
     
  7. richierich1204

    richierich1204

    Joined:
    May 20, 2010
    Posts:
    45
    Hey, I was running into this very same issue with NAN reported for all my transitions, rotations and L2W component data. Turns out the rotation data was garbage (I wasn't setting it, so it was all 0's). Once I initialized the rotation value with quaternion.identity the issue resolved itself.
     
    Ksanone, nicloay and schaefsky like this.
  8. schaefsky

    schaefsky

    Joined:
    Aug 11, 2013
    Posts:
    90
    @MartinTilo @Adam-Mechtley
    Ok so I think I found something, wanted to submit a bug report with Unity but I always get a "Connection closed" error when the sending is supposed to be done:
    So here is my bullet prefab
    upload_2020-4-27_20-24-10.png
    In my scene I have a GameObject to convert it:
    upload_2020-4-27_20-25-14.png
    Code (CSharp):
    1. using Unity.Entities;
    2.  
    3. [GenerateAuthoringComponent]
    4. public struct BulletSpawnerInfo : IComponentData
    5. {
    6.     public Entity Prefab;
    7.     public float Speed;
    8.     public float TTL;
    9.     public float Damage;
    10. }
    11.  
    When I hit play, the Prefab Entity looks like this (with physics 0.2.5, Unity 2019.3.5f1):
    upload_2020-4-27_20-31-10.png

    With Physics > 0.2.5; 0.3.2 in this case and Unity 2020.1.0b6.3521 (but also happened with Unity 2019) it looks like this, notice Physicsmass rot and InverseInertia:

    upload_2020-4-27_20-27-49.png
    Spawning bullets gives the effect described in my first post, also now gives ""Invalid AABB a" in console.

    Now if I check this:
    upload_2020-4-27_20-35-27.png
    and leave default values the converted prefab when I hit play looks like this (continued...):
     
    Last edited: Apr 27, 2020
  9. schaefsky

    schaefsky

    Joined:
    Aug 11, 2013
    Posts:
    90
    upload_2020-4-27_20-37-19.png
    and my spawned bullets look like this:
    upload_2020-4-27_20-38-3.png

    So everything seems to be working again.
    I think there is something wrong with the PhysicsMass when you do not overrride the mass distribution.

    Thanks @richierich1204 you send me on the right track, although I am explicitly setting rotation in my spawn script as you can see above.
     
    richierich1204 likes this.
  10. Adam-Mechtley

    Adam-Mechtley

    Administrator

    Joined:
    Feb 5, 2007
    Posts:
    290
    @schaefsky Sorry for the delay getting back to this thread.

    I don't see any PhysicsShapeAuthoring component on the screen shots from your Inspector. Where is PhysicsCollider coming from in this case? What kind of collider is on this object?
     
  11. schaefsky

    schaefsky

    Joined:
    Aug 11, 2013
    Posts:
    90
    Sorry I have currently mutlitple issues with Unity, however it appears that only my old prefab shows that behaviour, new ones seem to work fine.

    The shape I had set on the child gameobject, forgot to post:
    upload_2020-5-6_23-11-28.png

    I will hace to try some more, but it seems the issue is not appearing when creating new prefabs.
     
  12. schaefsky

    schaefsky

    Joined:
    Aug 11, 2013
    Posts:
    90
    The bug seems to appear when Physics Body is on a root object, and the Physics Shape is on a child.
    Sorry, I should have included a screenshot of my actual prefab setup, I know how frustrating it is when vital info is missing in a bug report.

    I can still reproduce when creating a new prefab with similar settings.
    As long as "Is Trigger" is not set, it will spawn ok.
    Setting "Is Trigger" will break it.
    Clicking "Override default mass distribution" makes it work again.
    As a side note using a capsule collider instead of physics shape has the same effect regarding trigger and spawn bug.

    Now I tried moving the physics shape from the child object to the root object, then it works. Also same effect with capsule collider.

    upload_2020-5-7_22-4-4.png

    upload_2020-5-7_22-4-14.png
     
  13. Adam-Mechtley

    Adam-Mechtley

    Administrator

    Joined:
    Feb 5, 2007
    Posts:
    290
    Thanks I am able to reproduce it. It's likely related to a fix we made so that triggers would not affect mass properties. We'll get it fixed in the next version. For now you can continue to override mass properties to unblock yourself.
     
    nanobot_games and schaefsky like this.
  14. Resshin27

    Resshin27

    Joined:
    Apr 21, 2018
    Posts:
    31
    I am having the same issue with Latest Entities and Physics package on Unity 2021.1.0b6.
    I am getting NaNs on Translation, LTW and PhysicsVelocity.Linear values, if I don't set the PhysicsVelocity at spawn time.
    I set an initial Translation value which is also gobbled into NaNs.
    Rotation is quaternion.identity & PhysicsVelocity.Angular is zero as shown in Entities inspector.
    When I Disable the PhysicsShape component on my Prefab, then rerun the scene, spawn the Prefab, the NaNs are gone.
    If I spawn my prefab and assign it a non-zero PhysicsVelocity.Linear value straight away, then it works normally. Is this a bug or what?
     
  15. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hi @Resshn27 thanks for reporting this! Could we please confirm some things first, to make sure I understand the exact repro steps:
    - Which UP version are you using, is it 0.6.0?
    - Are PhysicsShape and PhysicsBody on the same game object or on parent and child?
    - Does the problem appear only for triggers?
    - How do you set PhysicsVelocity at spawn time, is it via editor UI or in the code?

    This thread is quite old and there were some amendments to repro steps , so I want to make sure I understand your problem and repro 100%.
     
  16. Resshin27

    Resshin27

    Joined:
    Apr 21, 2018
    Posts:
    31
    Hey!
    1. Yes, Unity Physics version 0.6.0
    2. PhysicsShape and PhysicsBody authoring are on the same gameobject. No child hierarchy.
    3. In my case, it is a spherical Ball prefab for my simple Breakout clone:
    Set as Dynamic/Kinematic (both gives NANs btw),
    Gravity factor set as 0,
    InverseMass as 1,
    InverseInertia as 0 (I am using UnityPhysicsSamples' SetInverseInertiaBehaviour for that).
    Initial Linear and Angular velocity are 0.
    Initial Transform of my prefab is all 0s in T & R, with only scale as (0.5, 0.5, 0.5)
    It is set to Collide, not Trigger.
    4. I solved my case by adding a PhysicsExclude to my ball during spawn-time first. And then removing the PhysicsExclude when I wanted to move it later.

    I parented my ball prefab to another Entity straight away after spawning it, so zero Physics velocity during spawn time. Later I remove parent & PhysicsExclude and provide it with a non-zero PhysicsVelocity through code.
    And spawn of prefab was done with ECB.ParallelWriter of BeginSimulationECBS

    I could not dig into the source code to check, but my experimentation gave me a picture that Translation, PhysicsVelocity.Linear & LTW NaNs are being generated when my Dynamic/Kinematic body prefab had both zero initial linear velocity & zero gravity factor;
    Rotation remains as (0,0,0,1) as per Entity debugger, so it's not related to Rotation data for sure.

    Will share my spawning code and snapshots of prefab with you as soon as I am back at home.
     
    Last edited: Mar 19, 2021
  17. Resshin27

    Resshin27

    Joined:
    Apr 21, 2018
    Posts:
    31
    Well, I am not able to reproduce this, with the same code as it was yesterday :eek:
    Well, I wonder where the issue actually was, I wasted my 1-2 hours figuring it out. Today it just works with the same code.:rolleyes:

    Here is the spawn code:

    Code (CSharp):
    1.             Entities.WithAll<BallSpawnerEnabledTag>().
    2.             ForEach((Entity entity, int entityInQueryIndex, in Spawner spawner, in BallSettings settings) =>
    3.                {
    4.                    var ballEntity = commandBuffer.Instantiate(entityInQueryIndex, spawner.Prefab);
    5.  
    6.                    // commandBuffer.AddComponent(entityInQueryIndex, ballEntity, new PhysicsExclude()); // Added to make Physics system behave well
    7.  
    8.                    commandBuffer.SetComponent(entityInQueryIndex, ballEntity, new Rotation { Value = quaternion.identity });
    9.                    commandBuffer.SetComponent(entityInQueryIndex, ballEntity, new Speed { Value = settings.InitialSpawnSpeed });
    10.  
    11.                    // Remove the tag component so that system only runs once at a time
    12.                    commandBuffer.RemoveComponent<BallSpawnerEnabledTag>(entityInQueryIndex, entity);
    13.  
    14.                    // Dont destroy the ball spawner, need it later to spawn balls again
    15.                    // commandBuffer.DestroyEntity(entityInQueryIndex, entity);
    16.                }).ScheduleParallel();
    Prefab:
    upload_2021-3-19_19-23-41.png

    Today's result, no NaNs :eek:

    upload_2021-3-19_19-25-8.png
     
  18. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Hmm, strange, that sounds like a timing/scheduling problem (maybe your Entities.Foreach runs after the system where buffer gets executed?). If you hit it again, it'd be ideal to share a minimal repro project or try to repro on one of the PhysicsSamples.
     
  19. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    540
    Thanks for your comment. Also helped in my case.