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 Enity moving, visual is not. ?? [Game View works, not Scene View]

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

  1. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663
    EDIT: The resolution was that Entities cannot be seen moving in Scene View, but they are visible in Game View.

    Edit #2: If you go to the menu EDIT > PREFRENCES. Then select the ENTITIES option, there is a section called "Baking", the option "Scene View Baking" Set this to "Runtime Data" to see entities move in Scene View. (Thanks to @Rukhanka for this info)

    Thanks to @Rukhanka (and to @Rubenz for the help too)


    -----------------------------
    I was following along with Code Monkey's latest ECS tutorial and while most everything with it is fine, Unity dropped TransformAspect. Some reading shows that the LocalTransform is the replacement that I need to affect.

    So I update my code as follows, but the thing is, within the Inspector I can watch as the Local and Local To World values update, however the visual object (sphere, sprite, whatever) does not move.

    I'm happy to post my code if anyone wants that, but since I can see the values for any of these entities are updating when I select that specific one within the Entities Hierarchy. So it feels like a render or some other dumbass thing I am doing wrong.

    Thanks for any info
     
    Last edited: Jun 15, 2023
  2. Rubenz

    Rubenz

    Joined:
    Feb 2, 2017
    Posts:
    8
    Could be lots of things. One thing that happened to me two days ago, was that the Entities.Physics package was somehow decoupling child objects from the parents. Everything worked before installing it, but after getting it I had to restructure a bit.

    But it is probably something simpler, could you maybe share a screenshot of your hierarchy and the inspector of the gameobject that should move? If there are any scripts attached to that gameobject, especially a baker, might help to see them, too.
     
  3. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663
    @Rubenz

    Thanks for the response. Here are two images: Edit and then Play modes: https://imgur.com/a/IZaI49s

    You can see by the red outline the start position and it's Target Position (Baker value) At runtime you can see the LocalTransform's position updated (also shown highlighted)


    Here is the basic code, a struct that moves items from one place to another. Simple.... and yet....

    At this point I'm getting more and more sure it's something REALLY dumb that I am doing. (feel free to point and laugh)

    Code (CSharp):
    1. public partial struct BattleBotMovementSystem : ISystem
    2. {
    3.     public void OnUpdate(ref SystemState state)
    4.     {
    5.         float deltaTime = SystemAPI.Time.DeltaTime;
    6.  
    7.         JobHandle jobHandle = new BattleBotMoveJob
    8.         {
    9.             deltaTime = deltaTime
    10.         }
    11.         .ScheduleParallel();
    12.  
    13.         jobHandle.Complete();
    14.     }
    15. }
    16.  
    17. public partial struct BattleBotMoveJob : IJobEntity
    18. {
    19.     public float deltaTime;
    20.  
    21.     public void Execute(MoveToPositionAspect moveToPositionAspect)
    22.     {
    23.         moveToPositionAspect.Move(deltaTime);
    24.     }
    25. }
    26.  
    27. --------------------
    28.  
    29. public readonly partial struct MoveToPositionAspect : IAspect
    30. {
    31.     private readonly RefRW<LocalTransform> localTransform;
    32.     private readonly RefRO<Speed> speed;
    33.     private readonly RefRW<TargetPosition> targetPosition;
    34.  
    35.     public void Move(float deltaTime)
    36.     {
    37.         float3 direction = math.normalize(targetPosition.ValueRO.value - localTransform.ValueRO.Position);
    38.  
    39.         localTransform.ValueRW.Position += direction * deltaTime * speed.ValueRO.value;
    40.  
    41.         //localTransform.ValueRW.Position = new float3(0, 0, 0); //yes, the values get set, but the visual does not move
    42.  
    43.     }
     
  4. Rubenz

    Rubenz

    Joined:
    Feb 2, 2017
    Posts:
    8
    Mhh, I don't see anything obvious. Three things come to mind though.

    First, which version of the Entities package are you on? Mine doesn't seem to have a ScheduleParallel() Version without arguments that returns a JobHandle. Does not seem to be in the documentation either. I had problems with jobHandle.Complete() in the past, could you try running it without the jobHandle just like:

    Code (CSharp):
    1. new BattleBotMoveJob
    2. {
    3.      deltaTime = deltaTime
    4. }.ScheduleParallel();
    Second, could you share the bakers maybe, the three files on your battle-bot?

    Third, do you have any errors or warnings?
     
    Last edited: Jun 13, 2023
  5. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663
    Version info:
    * Unity 2022.3.0
    * Entities 1.0.10

    All related entity packages are up to date.

    When I updated the code as you stated above, but with .Run I get the following error. I do not get it when running Schedule or ScheduleParallel

    I do have a handful of Warnings and the only one that makes me wonder is this vague one:
    Internal: JobTempAlloc has allocations that are more than the maximum lifespan of 4 frames old - this is not allowed and likely a leak

    To Debug, run app with -diag-job-temp-memory-leak-validation cmd line argument. This will output the callstacks of the leaked allocations.


    I'm gonna stop for the night. Maybe something will come to me.

    I also wanted to thank you again for your time

    ----------------------------------
    InvalidOperationException: The previously scheduled job LocalToWorldSystem:ComputeChildLocalToWorldJob reads from the ComponentTypeHandle<Unity.Transforms.LocalTransform> ComputeChildLocalToWorldJob.JobData.LocalTransformLookupRO. You are trying to schedule a new job BattleBotMoveJob, which writes to the same ComponentTypeHandle<Unity.Transforms.LocalTransform> (via BattleBotMoveJob.JobData.__TypeHandle.__MoveToPositionAspect_RW_AspectTypeHandle.MoveToPositionAspect_localTransformCAc). To guarantee safety, you must include LocalToWorldSystem:ComputeChildLocalToWorldJob as a dependency of the newly scheduled job.
    Unity.Jobs.LowLevel.Unsafe.JobsUtility.Schedule (Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& parameters) (at <6c9b376c3fca40b787e8c1a2133bf243>:0)
    Unity.Entities.JobChunkExtensions.ScheduleInternal[T] (T& jobData, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependsOn, Unity.Jobs.LowLevel.Unsafe.ScheduleMode mode, Unity.Collections.NativeArray`1[T] chunkBaseEntityIndices) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/IJobChunk.cs:322)
    Unity.Entities.JobChunkExtensions.RunByRef[T] (T& jobData, Unity.Entities.EntityQuery query) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/IJobChunk.cs:229)
    BattleBotMoveJob+InternalCompilerQueryAndHandleData.Run (BattleBotMoveJob& job, Unity.Entities.EntityQuery query) (at Unity.Entities.SourceGen.JobEntityGenerator/Unity.Entities.SourceGen.JobEntity.JobEntityGenerator/Temp/GeneratedCode/Assembly-CSharp/BattleBotMovementSystem__JobEntity_6923671380.g.cs:170)
    BattleBotMovementSystem.__ScheduleViaJobChunkExtension_0 (BattleBotMoveJob job, Unity.Entities.EntityQuery query, Unity.Jobs.JobHandle dependency, Unity.Entities.SystemState& state, System.Boolean hasUserDefinedQuery) (at Assets/HBC/Scripts/ECS/BattleBotMovementSystem.cs:45)
    BattleBotMovementSystem.OnUpdate (Unity.Entities.SystemState& state) (at Assets/HBC/Scripts/ECS/BattleBotMovementSystem.cs:13)
    BattleBotMovementSystem.__codegen__OnUpdate (System.IntPtr self, System.IntPtr state) (at <a278d51bbba44761a1a7a24be2a37df0>:0)
    Unity.Entities.SystemBaseRegistry.ForwardToManaged (System.IntPtr delegateIntPtr, Unity.Entities.SystemState* systemState, System.Void* systemPointer) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/SystemBaseRegistry.cs:371)
    Unity.Entities.SystemBaseRegistry.CallForwardingFunction (Unity.Entities.SystemState* systemState, Unity.Entities.UnmanagedSystemFunctionType functionType) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/SystemBaseRegistry.cs:340)
    Unity.Entities.SystemBaseRegistry.CallOnUpdate (Unity.Entities.SystemState* systemState) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/SystemBaseRegistry.cs:383)
    Unity.Entities.WorldUnmanagedImpl.UnmanagedUpdate$BurstManaged (System.Void* pSystemState) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/WorldUnmanaged.cs:855)
    Unity.Entities.WorldUnmanagedImpl+UnmanagedUpdate_0000153F$BurstDirectCall.Invoke (System.Void* pSystemState) (at <ea07472f9266468581a0ca6f82f23a2c>:0)
    Unity.Entities.WorldUnmanagedImpl.UnmanagedUpdate (System.Void* pSystemState) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/WorldUnmanaged.cs:828)
    Unity.Entities.WorldUnmanagedImpl.UpdateSystem (Unity.Entities.SystemHandle sh) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/WorldUnmanaged.cs:913)
    Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/ComponentSystemGroup.cs:729)
    UnityEngine.Debug:LogException(Exception)
    Unity.Debug:LogException(Exception) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/Stubs/Unity/Debug.cs:19)
    Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/ComponentSystemGroup.cs:740)
    Unity.Entities.ComponentSystemGroup:OnUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/ComponentSystemGroup.cs:693)
    Unity.Entities.SystemBase:Update() (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/SystemBase.cs:418)
    Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/ScriptBehaviourUpdateOrder.cs:526)
     
  6. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663
    I have simplified my code, deleted all ECS objects, recreated ONE and yet still get the same issue

    I cannot figure out why the entity values are updating, but the object (in this case a Capsule) is not moving in the scene

    Code (CSharp):
    1. public readonly partial struct BattleBotMoveAspect : IAspect
    2. {
    3.     private readonly RefRW<LocalTransform> localTransform;
    4.     private readonly RefRO<Speed> speed;
    5.     private readonly RefRW<TargetPosition> targetPosition;
    6.  
    7.     public void Move(float deltaTime)
    8.     {
    9.         float3 direction = math.normalize(targetPosition.ValueRO.value - localTransform.ValueRO.Position);
    10.  
    11.         localTransform.ValueRW.Position += direction * deltaTime * speed.ValueRO.value;
    12.     }
    13. }
    14.  
    15.  
    16. public partial struct BattleBotMovementSystem : ISystem
    17. {
    18.     public void OnUpdate(ref SystemState state)
    19.     {
    20.         float deltaTime = SystemAPI.Time.DeltaTime;
    21.  
    22.         new BattleBotMoveJob
    23.         {
    24.             deltaTime = deltaTime
    25.         }
    26.         .Schedule();
    27.     }
    28. }
    29.  
    30. public partial struct BattleBotMoveJob : IJobEntity
    31. {
    32.     public float deltaTime;
    33.  
    34.     public void Execute(BattleBotMoveAspect battleBotMoveAspect)
    35.     {
    36.         battleBotMoveAspect.Move(deltaTime);
    37.     }
    38. }
    39.  
    40.  
     
  7. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    177
    Not moving in scene view and game view, or only in scene view?
     
    Rubenz likes this.
  8. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663
    I haven't checked in Game View, thanks I'll check that in an hour and report back
     
    Rubenz likes this.
  9. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663
    @Rukhanka You're my hero!

    Yes, entities can be seen moving in Game View but not Scene View.

    Thank you!

    Have a wonderful weekend
     
  10. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    177
    You can make it moving in scene view too. Adjust this property:
    upload_2023-6-15_10-39-41.png
     
    JamesWjRose likes this.
  11. JamesWjRose

    JamesWjRose

    Joined:
    Apr 13, 2017
    Posts:
    663