Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Collisions Across Network

Discussion in 'Multiplayer' started by Highpro620, Aug 6, 2023.

  1. Highpro620

    Highpro620

    Joined:
    May 13, 2021
    Posts:
    1
    This code works when I playtest it on my computer but when I playtest with two computers it does not register the OnCollisionEnter.

    Code (CSharp):
    1. using UnityEngine;
    2. using Photon.Pun;
    3. using System.Collections.Generic;
    4.  
    5. public class TagMechanic : MonoBehaviourPunCallbacks
    6. {
    7.     public Transform teleportLocation;
    8.     public Material taggedMaterial;  
    9.     public Material untaggedMaterial;
    10.  
    11.     public bool isTagged = false;
    12.     public PhotonView pv;
    13.     public MeshRenderer meshRenderer;
    14.  
    15.     private void Start()
    16.     {
    17.         pv = GetComponent<PhotonView>();
    18.         meshRenderer = GetComponent<MeshRenderer>();
    19.         if (teleportLocation == null)
    20.             teleportLocation = GameObject.Find("Respawn Anchor").transform;
    21.  
    22.         if (PhotonNetwork.IsMasterClient && pv.IsMine)
    23.         {
    24.             pv.RPC("OnTagged", RpcTarget.AllBuffered);
    25.         }
    26.     }
    27.  
    28.     public void ButtonTagTest()
    29.     {
    30.         if (pv.IsMine)
    31.         {
    32.             TagMechanic[] tms = FindObjectsByType<TagMechanic>(FindObjectsSortMode.None);
    33.             foreach (TagMechanic tm in tms)
    34.             {
    35.                 if (tm != this)
    36.                 {
    37.                     TagOtherPlayer(tm);
    38.                 }
    39.             }
    40.         }
    41.     }
    42.  
    43.     private void OnCollisionEnter(Collision collision)
    44.     {
    45.        
    46.         TagMechanic otherPlayer = collision.transform.GetComponent<TagMechanic>();
    47.    
    48.         if (otherPlayer != null)
    49.         {
    50.             if (pv.IsMine)
    51.             {
    52.                 if (isTagged)
    53.                     TagOtherPlayer(otherPlayer);
    54.             }
    55.         }
    56.    
    57.     }
    58.  
    59.     private void TagOtherPlayer(TagMechanic otherPlayer)
    60.     {
    61.         pv.RPC("OnUntagged", RpcTarget.AllBuffered);
    62.         otherPlayer.pv.RPC("OnTagged", RpcTarget.AllBuffered);
    63.  
    64.     }
    65.  
    66.     [PunRPC]
    67.     public void OnTagged()
    68.     {
    69.  
    70.         isTagged = true;
    71.  
    72.         if (pv.IsMine)
    73.             transform.position = teleportLocation.position;
    74.  
    75.         meshRenderer.material = taggedMaterial;
    76.     }
    77.  
    78.     [PunRPC]
    79.     public void OnUntagged()
    80.     {
    81.         isTagged = false;
    82.         meshRenderer.material = untaggedMaterial;
    83.     }
    84. }
    85.  
     
  2. NoelStephens_Unity

    NoelStephens_Unity

    Unity Technologies

    Joined:
    Feb 12, 2022
    Posts:
    48
    @Highpro620
    MonoBehaviourPunCallbacks is a Photon specific class, so I can't help you troubleshoot issues with that specifically.

    It could be, speaking in the context of Netcode for GameObjects (NGO), due to relative context and authority. You might find this response to another physics related question helpful. With NGO, the most likely cause (if you are using Rigidbody and NetworkRigidbody) is that the non-authoritative side/instance sets its Rigidbody as Kinematic which will end up ignoring the associated collider. I would highly recommend reading the response in the link I provided above as it does cover this specific issue.
    The NGO relative high-level overview:
    • Host (Server Authoritative Model)
      • Object-A:
        • Components: NetworkObject, NetworkRigidbody, Rigidbody, NetworkTransform
        • Is not Kinematic because this is the host-server side instance.
        • Collisions occur on this object
    • Client
      • Object-A Instance:
        • Is Kinematic because the client is not the authority of the object.
        • Collisions will not occur on this object since the authority (host-server) conveys the end result of interactions (i.e. changes to the transform) via the NetworkTransform.
    There could be scenarios where you might need to know if a non-authoritative instance has entered into or collided with something on the non-authoritative side. You can accomplish this by adding a trigger collider (i.e. an additional collider set as a trigger). However, you should be cautious about this approach as you can run into race conditions where non-authoritative instances are trying to handle something the authoritative instance should be handling.

    Of course, this could very well not have anything to do with why it is not working with Photon...