What is wrong here?

Hi, I’ve been looking on entity queries however for some reason I can’t change the velocity of the player. First I created an entity query, something like this;

            _entityQuery = new EntityQueryBuilder(Allocator.Temp)
                .WithAll<PlayerInput, PlayerMovement>()
                .WithAllRW<PhysicsVelocity>()
                .Build(this);

And then, inside the update method I did something like this;

            var inputs = _entityQuery.ToComponentDataArray<PlayerInput>(Allocator.Temp);
            var movements = _entityQuery.ToComponentDataArray<PlayerMovement>(Allocator.Temp);
            var velocities = _entityQuery.ToComponentDataArray<PhysicsVelocity>(Allocator.Temp);
          
            for (var i = 0; i < inputs.Length; i++)
            {
                var input = inputs[i];
                var movement = movements[i];
                var velocity = velocities[i];
              
                velocity.Linear = new float3
                {
                    x = input.MovementInput.x * movement.Speed,
                    y = input.MovementInput.y * movement.Speed,
                    z = 0
                };
              
                velocities[i] = velocity;

The problem is that the velocity is not being applied. I’ve added the system to the FixedStepSimulationSystemGroup and marked it to update before the PhysicsSystemGroup.

After failing I decided to try it in another way, and this is how I got it done;

            Entities.ForEach((
                ref PhysicsVelocity velocity,
                in PlayerInput input,
                in PlayerMovement movement) =>
            {
                velocity.Linear = new float3
                {
                    x = input.MovementInput.x * movement.Speed,
                    y = input.MovementInput.y * movement.Speed,
                    z = 0
                };
            }).Schedule();

However, I am still curious about the first one. What was the reason that I wasn’t able to write to the PhysicsVelocity?

The first version you provided uses new buffers that are merely copies of the original chunk data. Writes to those arrays are only writes to a copy of the data, not the original. The second way is pretty much the default way you should have gotten it done.

1 Like