#I'm following this official tutorial about netcode for entities

1 messages · Page 1 of 1 (latest)

red patio
#

Post you code here. You should be able to CubeMovementSystem in the editor to see what's running on it

hexed sierra
#
using Unity.Burst;
using Unity.Entities;
using Unity.NetCode;
using UnityEngine;

public struct CubeInput : IInputComponentData
{
    public int Horizontal;
    public int Vertical;

    public void OnStart()
    {
        Debug.Log("A");
    }
}

[DisallowMultipleComponent]
public class CubeInputAuthoring : MonoBehaviour
{
    class CubeInputBaking : Unity.Entities.Baker<CubeInputAuthoring>
    {
        public override void Bake(CubeInputAuthoring authoring)
        {
            Debug.Log("B");
            var entity = GetEntity(TransformUsageFlags.Dynamic);
            AddComponent<CubeInput>(entity);
        }
    }
}

[UpdateInGroup(typeof(GhostInputSystemGroup))]
public partial struct SampleCubeInput : ISystem
{
    public void OnCreate(ref SystemState state)
    {
        Debug.Log("C");
        state.RequireForUpdate<NetworkStreamInGame>();
        state.RequireForUpdate<CubeSpawner>();
    }

    public void OnUpdate(ref SystemState state)
    {
        Debug.Log("D");
        foreach (var playerInput in SystemAPI.Query<RefRW<CubeInput>>().WithAll<GhostOwnerIsLocal>())
        {
            playerInput.ValueRW = default;
            if (Input.GetKey("left"))
                playerInput.ValueRW.Horizontal -= 1;
            if (Input.GetKey("right"))
                playerInput.ValueRW.Horizontal += 1;
            if (Input.GetKey("down"))
                playerInput.ValueRW.Vertical -= 1;
            if (Input.GetKey("up"))
                playerInput.ValueRW.Vertical += 1;
        }
    }
}
#

Or maybe you want as a file ?

red patio
#

Do you also have CubeMovementSystem?

hexed sierra
#

Yes, here it is

#
using Unity.Entities;
using Unity.Mathematics;
using Unity.NetCode;
using Unity.Transforms;
using Unity.Burst;
using UnityEngine;

[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
[BurstCompile]
public partial struct CubeMovementSystem : ISystem
{

    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        var speed = SystemAPI.Time.DeltaTime * 4;
        foreach (var (input, trans) in SystemAPI.Query<RefRO<CubeInput>, RefRW<LocalTransform>>().WithAll<Simulate>())
        {
            var moveInput = new float2(input.ValueRO.Horizontal, input.ValueRO.Vertical);
            moveInput = math.normalizesafe(moveInput) * speed;
            trans.ValueRW.Position += new float3(moveInput.x, 0, moveInput.y);
        }
    }
}
red patio
#

That all should work. You can also check the entity inspector to see if the player cube has the correct components

hexed sierra
#

Should it have CubeInputAuthoring as componentin ghost ?

red patio
#

Nope. As long as Cube has CubeInput and LocalTransform, the CubeMovementSystem should act on it

hexed sierra
#

Okay. And I checked, CubeInput and LocalTransform are also in Cube of ClientWorld

red patio
#

Make sure its also the same in the Server World

hexed sierra
#

It is (screenshots are in ServerWorld)

#

So yeah, i don't really understand. It is not a project from older version updated to 6, soooo

red patio
hexed sierra
#

I will try by cloning it then putting the classes on a fresh one

hexed sierra
#

It works on the cloned repo (Some delays for the client with many large serverTick error).

hexed sierra
#

Copying the files to a new project doesn't seem to work. No errors, but I don't see any cube spawning (ingame nor in entity hierarchy)

hexed sierra
#

Second try, still not succeeding upon transfering files to a new project

hexed sierra
#

Ok it works !

It was a silly error from me alone. Here's the thing:

  • By clicking on CubeSpawnerAuthoring component on the Spawner GameObject, it showed me the location of the deprecated CSA from Unity, but not the one I made from the tutorial
  • I tried to force add my CubeSpawnerAuthoring but i got a message that meant "must be a MonoBehavior" or something like that.
  • After a quick search, it appears it is because my name file was different from the class and yes, I named my file CubeSpawnINGAuthoring.
  • After a quick rename, the movement works perfectly.