DOTS. Only the `Local Transform` in `runtime inspector` changes, while the `Cube` in the `Game` didn't move

  • com.unity.entities 1.2.3

I’m new in DOTS and find trouble with the simple example.
I use a SpeedManager to try interation between MonoBehaviour and Entities. But I find that only the metrics in runtime inspector changes(like Local Transform and Local to World). However, the Cube in the Game didn’t move.

I try modify the value in the Authoring Inspector of Speed Authoring. The Cube finally moves in the game view. But the Console output still show that the speed are setting to 2, and local transform are moving towards right. Actually, in the Game view, the Cube is moving at a speed of -0.2.

And this is the ‘Mixed Inspector’ showing.

Class SpeedManager .

using UnityEngine;
using Unity.Entities;
using Unity.Collections;
using TinyRTS;
using Unity.Transforms;
using UnityEngine.UIElements;

namespace TinyRTS
{
    public class SpeedManager : MonoBehaviour
    {
        public float global_speed;
        private void Start()
        {
            Debug.Log("SpeedManager Start!!");
            global_speed = 2;

        }
        private void Update()
        {
            var entities = World.DefaultGameObjectInjectionWorld.EntityManager.CreateEntityQuery(typeof(Speed)).ToEntityArray(Allocator.Temp);
            Debug.Log("entities.Length: " + entities.Length + " global_speed: " + global_speed);

            foreach (var entity in entities)
            {
                var speed = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<Speed>(entity);
                // Debug.Log("speed.value ori: " + speed.value);
                speed.value = global_speed;
                World.DefaultGameObjectInjectionWorld.EntityManager.SetComponentData(entity, speed);
                // speed = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<Speed>(entity);
                // Debug.Log("speed.value after: " + speed.value);

                var transform = World.DefaultGameObjectInjectionWorld.EntityManager.GetComponentData<LocalTransform>(entity);
                Debug.Log("transform.Position" + transform.Position);
            }
        }
    }
}

class `MovingAspect

using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using Unity.Burst;

namespace TinyRTS{
    public readonly partial struct MovingAspect : IAspect
    {
        private readonly Entity entity;
        private readonly RefRO<Speed> speed;
        private readonly RefRW<LocalTransform> transform;

        public void Move(float deltaTime)
        {
            transform.ValueRW.Position += new float3(1f, 0, 0) * speed.ValueRO.value * deltaTime;
        }
    }

    [BurstCompile]
    public partial struct MovingISystem : ISystem
    {
        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            new MoveJob { deltaTime = SystemAPI.Time.DeltaTime }.ScheduleParallel();
        }
    }

    [BurstCompile]
    public partial struct MoveJob : IJobEntity
    {
        public float deltaTime;
        [BurstCompile]
        public void Execute(MovingAspect aspect)
        {
            aspect.Move(deltaTime);
        }
    }
}

class SpeedAuthoring

using Unity.Entities;
using UnityEngine;
namespace TinyRTS
{
    public class SpeedAuthoring : MonoBehaviour
    {
        public float value;
    }

    public struct Speed : IComponentData
    {
        public float value;
    }

    public class SpeedBaker : Baker<SpeedAuthoring>
    {
        public override void Bake(SpeedAuthoring authoring)
        {
            Entity entity = GetEntity(authoring, TransformUsageFlags.Dynamic);
            AddComponent(entity, new Speed { value = authoring.value });
        }
    }
}


Result of editing the value of Authoring Inspector. The object is moving left at a speed of 0.2.

It looks like your project has netcode for entities installed. That would mean your application in the editor can run both a server world and a client world, which are independent worlds that model the authoritative simulation and a user-facing synchronized simulation respectively. The client world supports rendering features (through PresentationSystemGroup), the server world does not. You’re probably referencing the server world when you use World.DefaultGameObjectInjectionWorld which would be changing the speed values in the server world’s entities and thus making the [invisible] objects in your server world move, but you don’t affect the setup in the client world which means the loaded entities in the client world (that can all be seen) won’t move the way the loaded entities in the server world (that can’t all be seen) do.
If you’re not intending on using netcode, you would remove the package and see expected behavior. Otherwise, you need to read up on the netcode docs and the netcode samples for getting synchronization working.

2 Likes

That’t the true reason! Thank you so much!

I will go to read the documents of Netcode, for future work.