Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug reading position from LocalToWolrd geting NaNf NaNf NaNf

Discussion in 'Entity Component System' started by tzxbbo, Sep 3, 2023.

  1. tzxbbo

    tzxbbo

    Joined:
    Dec 14, 2019
    Posts:
    57
    I'm making a very basic shooting system, I loop through all guns and spawn a projectile on them something like this

    foreach(var (..., ..., ltw) in SystemAPI.Query<..., ..., RefRO<LocalToWorld>>)
    {
    Entity projectile = ecb.Instantiate(projectilePfb);
    ecb.SetComponent(projectile, new LocalTransform()
    {
    Position = ltw.ValueRO.Position,
    Rotation = ltw.ValueRO.Rotation
    };
    }


    Not sure why but only the very first projectile is spawned correctly and I couldn't find the rest of the projectiles.
    After doing a Debug.log I found out only the first time I read the correct value of LocalToWorld Postion, then the position somehow become float3(NaNf, NaNf, NaNf).

    Only the first time, LocalToWorld Position is shown correct, I don't know if this is a bug or I'm doing something wrong.
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    274
    The Scale is not set in your object initializer, so it will be the C# default 0. You must set scale explicitly, or use one of the static creation methods in LocalTransform.
    https://docs.unity3d.com/Packages/com.unity.entities@1.0/api/Unity.Transforms.LocalTransform.html
     
    apkdev and xVergilx like this.
  3. tzxbbo

    tzxbbo

    Joined:
    Dec 14, 2019
    Posts:
    57
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,623
    -edit- somehow blind on phone

    Don't use new localtransform as that will set a 0 quaternion which is nan.

    Instead use LocalTransfrom.FromPosition or one of the other methods and it'll setup all field defaults
     
    Last edited: Sep 4, 2023
  5. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    274
    They quite explicitly set the rotation from an existing value in that code snippet.
     
  6. tzxbbo

    tzxbbo

    Joined:
    Dec 14, 2019
    Posts:
    57
    okay, it's due to some other conflicting system, it's not a bug...