#Struggling to get started with basic networked physics

1 messages · Page 1 of 1 (latest)

rocky pulsar
#

Unity 6.0.47f1 (if you tell me to update without a relevant reason I'm biting)
Unity Physics 1.3.14
NCE 1.3.6
Entities 1.3.14

Basically I'm struggling to work out how to do physics properly at all. I find watching video tutorials outdated or hard to concentrate on and anything readable either doesn't actually provide an example of usage (unity docs) or is outdated (ecs 0.5), and AI is doing typical AI things and making more problems than its fixing...

[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]
[BurstCompile]
partial struct CubeMovementSystem : ISystem
{
    [BurstCompile]
    public void OnCreate(ref SystemState state)
    {
        state.RequireForUpdate<PhysicsWorldSingleton>();
        state.RequireForUpdate<NetworkTime>();
    }
    [BurstCompile]
    public void OnUpdate(ref SystemState state)
    {
        var speed = SystemAPI.Time.DeltaTime * 4;
        var physicsWorld = SystemAPI.GetSingleton<PhysicsWorldSingleton>().PhysicsWorld;
        var networkTime = SystemAPI.GetSingleton<NetworkTime>();
        foreach (var (input, owner, trans, predicted, entity) in SystemAPI.Query<RefRW<PlayerInput>, RefRO<GhostOwner>, RefRW<LocalTransform>, RefRO<PredictedGhost>>().WithAll<Simulate>().WithEntityAccess())
        {
            var moveInput = input.ValueRO.Move;
            moveInput = moveInput * speed;
            trans.ValueRW.Position += new float3(moveInput.x, 0, moveInput.y);
            if (input.ValueRO.Jump)
            {
                if (SystemAPI.HasComponent<PhysicsVelocity>(entity) &&
                SystemAPI.HasComponent<PhysicsMass>(entity))
                {
                    int rigidBodyIndex = physicsWorld.GetRigidBodyIndex(entity);
                    float3 impulse = new float3(0, 5f, 0);
                    float3 point = trans.ValueRW.Position;

                    Debug.Log("Jump applied");

                    physicsWorld.ApplyImpulse(rigidBodyIndex, impulse, point);

                    input.ValueRW.Jump = false;
                }
            }
        }
    }
   
}

this is my current movement system, which is probably wrong in so many ways, and my input is a modified version of https://gist.github.com/JohnnyTurbo/f6dd1b6623fd9d17d2141091f8066825 that takes into account netcode behaviour.

Problem:
When I press space I get all the logs printed (although "Jump Applied" logs like 20 times even though it's a single client test) but the cube does not actually jump. It spawns in the air and falls down so physics is definitely otherwise working and automatically simulating.

What I want:
Direct transform based controls for WASD and physics based jumping so I can learn the basics of how this works and start making actual projects

Gist

Code used in ECS Input System tutorial video on the Turbo Makes Games YouTube Channel - https://youtu.be/bFHvgqLUDbE - FireProjectileSystem.cs

nimble knot
#

your first problem is an input, you need to learn how to deal with networked input
start with https://github.com/Unity-Technologies/CharacterControllerSamples/blob/master/OnlineFPS/Assets/Scripts/Player/FirstPersonPlayerSystems.cs sample
next thing - prediction, it's hard to understand it when you implement it first time so try to follow two small practices:
your code should rely on Simulate tag because netcode will disable/enable it frequently for it's own needs
write to and only to ghost components because netcode knows how to backup and restore values of them

and remember that your predicted code can be called many, MANY times during one frame due to nature of prediction and next frame will rollback and call many times again so keep it as simple as possible

GitHub

Sample projects for the Unity.CharacterController package - Unity-Technologies/CharacterControllerSamples

rocky pulsar
#

I've skimming over the code and yeah this solution to inputs is very different to stuff I'd been seeing/basing off of so for sure going to go over all this in more detail.
I'm seeing something that tells the character to do physics checks I think, but nothing that says what physics changes should be made to do the checks with, so could you point me to whatever is applying forces/impulses/velocity changes?