Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Possible Bug with raycasts and collisions netcode for gameobjects

Discussion in 'Netcode for GameObjects' started by codeBatt, May 18, 2023.

  1. codeBatt

    codeBatt

    Joined:
    Feb 12, 2020
    Posts:
    40
    Hello dear unity officials,

    I am developing a third person shooter game using netcode for gameobjects. However, I have the issue where client can shoot host and decrease its health whereas, host 1 of 1000 times can hit and damage client even if they are standing on the ground. Also, clients can also can not hit each other with raycast eventhough ray is hit something else behind them.
    I wonder, ray is like totally ignoring the players, I removed and checked with layermasks and that does not change anything. I am using Unity's playerownershooterServerRpc() method which I modify it to take two local vector3 parameters to shoot. Its perfectly accurate to environment and other non-spawned objects. However, players can not hit each other except for clients. I am providing the video and the working raycast,
    Client -> Host-OK
    Host->Client-NOT OK
    Client-> Client- NOT OK
    It seems like players or colliders are not there.

    Please help me with this problem I am losing my mind for 2 weeks.
     
    Last edited: May 20, 2023
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi, are you 100% sure the LayerMask is set properly on both servers and clients?

    If yes, I'd recommend to debug with `Debug.DrawRay` to have a visual representation of where the raycasts are going on both instances
     
  3. codeBatt

    codeBatt

    Joined:
    Feb 12, 2020
    Posts:
    40
    Hello,
    I even removed layermasks to make sure that its "hit"-able, then I check with debug.log(hit.collider.name)
    It gave the true answers even with the very close objects.

    It still does not work for the ones as I mentioned. Player is like a ghost that is unshootable.Losing my mind
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,511
    Before that happens, try making an isolated test project. Often it's hard to see the actual issue within the current project and it becomes more obvious in a dedicated test case.

    Anyhow, I would also enable gizmo drawing of colliders and/or Windows => Analysis => Physics Debugger and connect to the host from within the editor (best to use ParrelSync so both run from the editor).

    If you use a NetworkRigidbody, the bodies will be set to Kinematic for clients. Not sure if or how this affects Raycast collisions but it's worth keeping in mind. And since all of your clients are unhittable, it seems the most likely culprit, next to client players running some special logic that removes or disables colliders somehow, or the code that processes collision detection.
     
  5. codeBatt

    codeBatt

    Joined:
    Feb 12, 2020
    Posts:
    40
    I am using a character controller and synty's third person shooter movement which moves character with animation and root motion. Is this because why its can not be hit? Even if yes, how the host can be hit by client so easily? I will try what you say in your comment thanks for response
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,511
    That might be a viable theory to pursue, that the root motion works correctly with physics/colliders only for the host but not for clients. I can imagine that the "offset" state of the root motion is not synchronized with/among clients. Should be easy to see when you spawn a client and have the collider visualization enabled. If true, the collider would stay where the client was spawned and won't move along with the character, or it may not even be there at all (check origin ie 0,0,0 in that case).
     
  7. codeBatt

    codeBatt

    Joined:
    Feb 12, 2020
    Posts:
    40
    Thank you code smile! you saved my life I found the issue and solved it.
    After synchronizing rootMotion vector via server rpc() raycast worked perfectly accurate. Thanks again for your reply.