Search Unity

Resolved How do I setup Physics for my use case

Discussion in 'NetCode for ECS' started by vectorized-runner, Apr 12, 2021.

  1. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    398
    I need Physics on the Client side for raycasting, Client can have authority on this since it's just sending which Entity it wants to target (which Server doesn't need to correct). I need Physics on the Server for collision detection, but I've heard it's not compatible with Prediction.

    I also tested moving a Cube with PhysicsVelocity in GhostPredictionSystemGroup and it was very laggy, whereas rolling my own Velocity component and system to translate the cube does not lag at all. But I need that collision detection for gameplay logic, so what are my options here?
     
    Lontis likes this.
  2. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    The limitation we have in entities 0.6 is that you cannot run physics simulation as part of the prediction loop. As long as all the physics simulation happens on the server and it sends the positions the client uses it should be fine. If you need some approximation of motion without collisions you could use a client-only system in the prediction loop which updates the position based on the velocity without any collision logic - but it is obviously a very rough approximation.

    If you want the ghosts to be possible to raycast against on the client side the easiest is to make them kinematic with physics velocity 0 on the client. You can do that by creating a client-only system which adds `new Unity.Physics.PhysicsMassOverride{IsKinematic = 1}` to everything which has a `PhysicsVelocity` and `GhostComponent`.
    You would also need to be careful to make sure PhysicsVelocity is zero on the client or the ghosts will be moved by physics. If you never synchronize PhysicsVelocity it should stay zero, so easiest might be to use a separate component for velocity if you need it for some rough approximation of prediction on the client.
     
  3. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    398
    Ok, so I need to do
    1- Initially make Ghosts kinematic and set PhysicsVelocity to zero, on ServerSimulationGroup
    2- Write to PhysicsVelocity on Server, custom Velocity on Client, in a system that runs in PredictionGroup
    3- Custom VelocitySystem which translates the Ghost by Velocity also can run in PredictionGroup, only on Client

    After that I will be able to raycast on Client and do movement prediction, but I can't predict collisions, but they will be eventually synced to Server. Seems good to me