#Charatcer controller : Netcode and LocalTransform

1 messages · Page 1 of 1 (latest)

stuck shell
#

Hello, I was trying to make a simple npc thing that the server will own. And I simply added a translate test system:

[GhostComponent()]
public struct TranslationSpeed : IComponentData
{
    public float3 speed;
}

[BurstCompile]
partial struct TestSystem : ISystem
{
    public void OnUpdate(ref SystemState state)
    {
        float deltaTime = SystemAPI.Time.DeltaTime;

        foreach (var (transform, speed) in SystemAPI.Query<RefRW<LocalTransform>, RefRO<TranslationSpeed>>())
            transform.ValueRW = transform.ValueRO.Translate(speed.ValueRO.speed * deltaTime);
    }
}
[DisallowMultipleComponent]
public class TestAuthoring : MonoBehaviour
{
    public class Baker : Baker<TestAuthoring>
    {
        public override void Bake(TestAuthoring authoring)
        {
            Entity entity = GetEntity(TransformUsageFlags.Dynamic);

            AddComponent(entity, new TranslationSpeed() {  speed = new float3(0,0.3f,0) });
        }
    }
}

But when I build and run (is it possible to have several view of clients in the editor btw ?), the npc start moving from the start position for each connected player.
I tried to filter the system for only the system, and then I saw in the editor than the server localToWorld was moving but not the client one.
So does it means that localTransform is not synchronized for ghost entities ? Even if they are dynamic ?
Do I need to add some kind of Translate GhostComponent and then compute the localToWorld on each side ? Is it the "good" practice ?

Thanks for your time !

lone nymph
#

ltw mostly used by rendering and your server can drop it completely
your problem is probably with syncing transform
if this entity owned by server, your clients should sync it position (and possibly predict new positions with this speed component but try to implement basic stuff for start)
try to run this system by server only and have proper transform on clients
later move this system to prediction loop

#

check character controller's samples or netcode samples to learn how to sync transform

stuck shell
#

ltw is not used for physics too ?
I'm on the charactercontroller OnlineFPS sample. I'll check back the default Netcode samples !

#

The first one is from my project and the second one is from asteroid samples. Looks like I really should have the localTransform send to All and I just messed up somewhere.

#

Even if I create a simple cube with nothing, I can't change LocalTransform sending process. Is there some requirement to share the localTransform ?

#

When I create a simple cube in the asteroid samples, the LocalTransform is set to All... Damn maybe it's just the OnlineFPS sample which change this. Maybe a netcode default settings

stuck shell
#

Same problem with the Competitive Action Multiplayer template. What kind of code can turn this off ?

topaz warren
# stuck shell Even if I create a simple cube with nothing, I can't change LocalTransform sendi...

A Ghost by default sends the LocalTransform, and by default it's owned by the server as far as I know.
So I don't really understand the problem.
I added your code to my netcode testing setup, your the yellow cube.
Blue is the player and red are just interpolated server cubes too.
The system runs only on the server because of the flag I added, with the debug.log.

Is there something else you want then what you see in the video?

[WorldSystemFilter(WorldSystemFilterFlags.ServerSimulation)]
partial struct TestSystem : ISystem
{
    //[BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        float deltaTime = SystemAPI.Time.DeltaTime;

        foreach (var (transform, speed) in SystemAPI.Query<RefRW<LocalTransform>, RefRO<TranslationSpeed>>())
            transform.ValueRW = transform.ValueRO.Translate(speed.ValueRO.speed * deltaTime);

        UnityEngine.Debug.Log($"TestSystem.OnUpdate in world: {state.World.Name}");
    }
}
stuck shell
topaz warren
#

Also, Unity has a Multiplayer Play Mode asset, which seems to work with Entities too.
It completely eats up my 8 year old computer this way though, but it works.

topaz warren
stuck shell
stuck shell
#

I found the problem

 [CreateBefore(typeof(TransformDefaultVariantSystem))]
 public partial class DefaultVariantSystem : DefaultVariantSystemBase
 {
     protected override void RegisterDefaultVariants(Dictionary<ComponentType, Rule> defaultVariants)
     {
         defaultVariants.Add(typeof(LocalTransform), Rule.ForAll(typeof(DontSerializeVariant)));
         defaultVariants.Add(typeof(KinematicCharacterBody), Rule.ForAll(typeof(KinematicCharacterBodyGhostVariant)));
         defaultVariants.Add(typeof(CharacterInterpolation), Rule.ForAll(typeof(CharacterInterpolationGhostVariant)));
     }
 }

But not sure how to correct it. My guess is that the Kinematic Controller needs to remove the localTransform default serialization. But that's probably only for the entities using the kinematic character body... Or maybe anything Kinematic. But I guess nobody here will have the answer. I should try to ask the dev directly.

topaz warren
stuck shell