I am attempting to go from the netcode sample NetCube, some changes here and there, and now I can’t move my character anymore
It stopped working when I added physics body to the character, I’ve already “Update Component List + Generate Code” in the prefab.
using Unity.Burst;
using Unity.Entities;
using Unity.NetCode;
using Unity.Networking.Transport;
using Unity.Physics;
using UnityEngine;
public struct PlayerInput : ICommandData<PlayerInput> {
public uint Tick => tick;
public uint tick;
public int horizontal;
public int vertical;
public void Deserialize(uint tick, DataStreamReader reader, ref DataStreamReader.Context ctx) {
this.tick = tick;
horizontal = reader.ReadInt(ref ctx);
vertical = reader.ReadInt(ref ctx);
}
public void Serialize(DataStreamWriter writer) {
writer.Write(horizontal);
writer.Write(vertical);
}
public void Deserialize(uint tick, DataStreamReader reader, ref DataStreamReader.Context ctx, PlayerInput baseline,
NetworkCompressionModel compressionModel) {
Deserialize(tick, reader, ref ctx);
}
public void Serialize(DataStreamWriter writer, PlayerInput baseline, NetworkCompressionModel compressionModel) {
Serialize(writer);
}
}
public class PlayerSendCommandSystem : CommandSendSystem<PlayerInput> { }
public class PlayerReceiveCommandSystem : CommandReceiveSystem<PlayerInput> { }
[UpdateInGroup(typeof(ClientSimulationSystemGroup))]
public class SamplePlayerInput : ComponentSystem {
protected override void OnCreate() {
RequireSingletonForUpdate<NetworkIdComponent>();
RequireSingletonForUpdate<EnableDOTSMMOGhostReceiveSystemComponent>();
}
protected override void OnUpdate() {
var localInput = GetSingleton<CommandTargetComponent>().targetEntity;
if (localInput == Entity.Null) {
var localPlayerId = GetSingleton<NetworkIdComponent>().Value;
Entities.WithNone<PlayerInput>().ForEach((Entity ent, ref PlayerIDComponent player) => {
if (player.PlayerId == localPlayerId) {
PostUpdateCommands.AddBuffer<PlayerInput>(ent);
PostUpdateCommands.SetComponent(GetSingletonEntity<CommandTargetComponent>(), new CommandTargetComponent { targetEntity = ent });
}
});
return;
}
var input = default(PlayerInput);
input.tick = World.GetExistingSystem<ClientSimulationSystemGroup>().ServerTick;
if (Input.GetKey("a"))
input.horizontal -= 1;
if (Input.GetKey("d"))
input.horizontal += 1;
if (Input.GetKey("s"))
input.vertical -= 1;
if (Input.GetKey("w"))
input.vertical += 1;
var inputBuffer = EntityManager.GetBuffer<PlayerInput>(localInput);
inputBuffer.AddCommandData(input);
}
}
[BurstCompile]
[UpdateInGroup(typeof(GhostPredictionSystemGroup))]
public class MovePlayerSystem : JobComponentSystem {
protected override Unity.Jobs.JobHandle OnUpdate(Unity.Jobs.JobHandle handle) {
handle.Complete();
var group = World.GetExistingSystem<GhostPredictionSystemGroup>();
var tick = group.PredictingTick;
var deltaTime = Time.DeltaTime;
Entities.ForEach((DynamicBuffer<PlayerInput> inputBuffer, ref PhysicsVelocity vel, ref PlayerAcceleration acc, ref PlayerMaxSpeed speed, ref PredictedGhostComponent prediction) => {
if (!GhostPredictionSystemGroup.ShouldPredict(tick, prediction)) {
Debug.Log(World.Name + " did not predict");
return;
}
PlayerInput input;
inputBuffer.GetDataAtTick(tick, out input);
if (input.horizontal > 0)
vel.Linear.x = Mathf.Clamp(vel.Linear.x + deltaTime * acc.Acceleration, -speed.MaxSpeed, speed.MaxSpeed);
else if (input.horizontal < 0)
vel.Linear.x = Mathf.Clamp(vel.Linear.x - deltaTime * acc.Acceleration, -speed.MaxSpeed, speed.MaxSpeed);
if (input.vertical > 0)
vel.Linear.z = Mathf.Clamp(vel.Linear.z + deltaTime * acc.Acceleration, -speed.MaxSpeed, speed.MaxSpeed);
else if (input.vertical < 0)
vel.Linear.z = Mathf.Clamp(vel.Linear.z - deltaTime * acc.Acceleration, -speed.MaxSpeed, speed.MaxSpeed);
Debug.Log(World.Name + " did predict: " + input.horizontal + ", " + input.vertical + vel.Linear);
}).WithoutBurst().Run();// (handle);
return default;
}
}
Last 8 lines of the console, for some reason the console shows 4 servers then 4 clients then repeats rather than 1 client 1 server repeat
ServerWorld did predict: 1, 1float3(0.1666656f, -1.8441E-06f, 0.1666667f)
ServerWorld did predict: 1, 1float3(0.1666675f, -1.68932E-06f, 0.1666666f)
ServerWorld did predict: 1, 1float3(0.1666656f, -1.842527E-06f, 0.1666667f)
ServerWorld did predict: 1, 1float3(0.1666675f, 2.041573E-06f, 0.1666666f)
ClientWorld0 did predict: 1, 1float3(5f, 1.57506f, 5f)
ClientWorld0 did predict: 1, 1float3(5f, 1.57506f, 5f)
ClientWorld0 did predict: 1, 1float3(5f, 1.57506f, 5f)
ClientWorld0 did predict: 1, 1float3(5f, 1.57506f, 5f)
The Project: https://www.mediafire.com/file/7ll2s2dz12nvf9s/Untituled_DOTS_MMO.zip/file
