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

Resolved Setting LocalTransform makes entity not visible

Discussion in 'Entity Component System' started by JamesWjRose, Jul 13, 2023.

  1. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663
    Sigh. I HATE these moments. I don't know why, but now It works. ¯\_(ツ)_/¯

    This works, for what it's worth considering the source
    Code (CSharp):
    1.            
    2. float3 position = new float3(1985, layer, -1200);
    3.            
    4. var localTransform = LocalTransform.FromPositionRotation(position, quaternion.identity);
    5. entityCommandBuffer.SetComponent(entity, localTransform);
    6.  
    ----------------------
    Ok,

    Welcome back to "James is doing something stupid"

    I have narrowed down the problem I have when I instantiate some objects when the game starts.

    Code (CSharp):
    1.  
    2. int index = randomComponent.ValueRW.random.NextInt(0, trafficSpawnerComponent.Length);
    3. Entity spawnedEntity = EntityManager.Instantiate(trafficSpawnerComponent.ElementAt(index).prefab);
    4.  
    5. float3 position ;
    6. position.y = 70;
    7. position.z = -1200;
    8. position.x = 1985;
    9.  
    10. var localTransform = LocalTransform.FromPositionRotation(position, quaternion.identity);
    11.  
    12. //ANY OF THESE CAUSE THE ENTITY TO DISAPPEAR
    13. //entityCommandBuffer.SetComponent(spawnedEntity, new LocalTransform { Position = position });      //entityCommandBuffer.SetComponent(spawnedEntity, localTransform);
    14. //entityCommandBuffer.SetComponent(spawnedEntity, new LocalTransform { Position = position, Rotation = new quaternion(), Scale = 1.0f });
    15.  
    16.  
    Within the Entities Hierarchy the entity's values are visible and set correctly:

    https://imgur.com/a/Si2c8E7

    This is a simple sphere and unlit material. Again, if I run this without attempting to set its position, the entity is visible in game and scene views.

    So.... what dumbass thing am I doing wrong?

    Thanks
     
    Last edited: Jul 13, 2023
  2. StickyKevin

    StickyKevin

    Joined:
    Jul 1, 2020
    Posts:
    22
    On Line 14 of your example, you should set the Rotation to quaternion.identity.
    Even though I am not on the 1.0 Transform version, I can imagine a zeroed quaternion can result into an "invalid" transform matrix which can't be rendered.
     
  3. Ayr

    Ayr

    Joined:
    Nov 8, 2014
    Posts:
    16
    Code (CSharp):
    1.  Entity instance = state.EntityManager.Instantiate(spawner.ValueRO.Prefab);
    2.                             //ecb.AddComponent(instance, new Parent { Value = entity });
    3.                             ecb.AddComponent(instance, new LocalTransform
    4.                             {
    5.                                 Position = SystemAPI.GetComponent<LocalToWorld>(entity).Position,
    6.                                 Rotation = quaternion.identity,
    7.                                 Scale = SystemAPI.GetComponent<LocalTransform>(spawner.ValueRO.Prefab).Scale
    8.                             });
    Hopefully this helps. You have to use LocaltoWorld. If you set a parent you don't need any positions.
     
    JamesWjRose likes this.
  4. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663

    I will look into this, thank you kindly.