Search Unity

Third Party Referencing a raycast hit from a client in Photon

Discussion in 'Multiplayer' started by Tyasu, Jul 4, 2019.

  1. Tyasu

    Tyasu

    Joined:
    Feb 19, 2019
    Posts:
    3
    I am having trouble getting the transformOfHit variable in this script to sync with other clients. Basically what I can see is happening is that the transformOfHit variable is reliant on the Ray "hit". Hit is set equal to Input.mousePosition so i'm pretty sure that the client that has "MyView set to true is using its own mouse position instead it thinks that I am not clicking on anything. If I am wrong please correct me but if not a solution to get a reference to a clients mouse position would be greatly appreciated as I have spent more than 10 hours over 2 days just trying to find a way to get the reference to this so I know what the client has clicked and thus set to its target. I am using this to fight the same enemy which is a scene object as other clients so they can both take its health down and kill it.

    I also think it might be worth mentioning that I have 2 cameras on the prefab I am instantiating for each player. If the PhotonView IsMine then I am activating the main camera and if not then I am activating the secondary camera so I can still have a camera that is capable of raycasting based on the mouse position on the client.

    Code (CSharp):
    1. public class Raycaster : MonoBehaviourPunCallbacks
    2. {
    3.     public PhotonView PV;
    4.  
    5.     private Transform camRotate;
    6.  
    7.     float currentMousePosition;
    8.     float pastMousePosition;
    9.  
    10.     float screenWidth;
    11.     float screenHeight;
    12.  
    13.     public Camera cam;
    14.     public Player playerScript;
    15.     public Transform transformOfHit;
    16.  
    17.     public RaycastHit hit;
    18.  
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         PV = GetComponent<PhotonView>();
    23.         playerScript = GetComponent<Player>();
    24.         screenWidth = Screen.width;
    25.         screenHeight = Screen.height;
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.         if (!PV.IsMine)
    32.         {
    33.             if (cam == null)
    34.             {
    35.                 cam = GetComponentInChildren<Camera>();
    36.             }
    37.             return;
    38.         }
    39.  
    40.         if (cam == null)
    41.         {
    42.             cam = GetComponentInChildren<Camera>();
    43.         }
    44.  
    45.         if (Input.GetMouseButtonDown(0))
    46.         {
    47.             // if left button pressed...
    48.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    49.             Debug.Log("This ray hit assigned in this raycaster");
    50.             PV.RPC("RpcSetHit", RpcTarget.OthersBuffered);
    51.          
    52.  
    53.  
    54.  
    55.             if (Physics.Raycast(ray, out hit))
    56.             {
    57.                 //Set transforOfHit to whatever the transform of the object that was hit
    58.                 PV.RPC("RpcSetTransformOfHit", RpcTarget.AllBuffered);
    59.  
    60.                 // the object identified by hit.transform was clicked
    61.                 // do whatever you want
    62.                 if (hit.transform.tag == "NPC")
    63.                 {
    64.                     print("You clicked an NPC");
    65.  
    66.                     //If there is already one created then dont create one
    67.                     if (GameObject.Find("ConvoBox(Clone)") == false)
    68.                     {
    69.                         //Flips the convobox after instantiation
    70.                         Quaternion hitQuaternion = hit.transform.rotation * Quaternion.Euler(0, 180f, 0);
    71.                         //Create the prefab
    72.                         Instantiate(Resources.Load("ConvoBox"), new Vector3(hit.transform.position.x,
    73.                             hit.transform.position.y + 5, hit.transform.position.z + 5), hitQuaternion);
    74.                     }
    75.                     else
    76.                         Destroy(GameObject.Find("ConvoBox(Clone)"));
    77.                 }
    78.  
    79.                 if (hit.transform.tag == "Teleporter")
    80.                 {
    81.                     print("You clicked a Teleporter");
    82.  
    83.                     if (hit.transform.name == "Flower Scene Teleporter")
    84.                     {
    85.                         //If there is already one created then dont create one
    86.                         if (GameObject.Find("FlowerSceneTeleporterConvo(Clone)") == false)
    87.                         {
    88.  
    89.                             Quaternion hitQuaternion = transform.rotation;
    90.                             //Create the prefab
    91.                             Instantiate(Resources.Load("FlowerSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    92.                                 hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
    93.  
    94.                             //Create the Prefab
    95.                             //Instantiate(Resources.Load(""), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    96.                         }
    97.                     }
    98.                     if (hit.transform.name == "Hell Scene Teleporter")
    99.                     {
    100.                         //If there is already one created then dont create one
    101.                         if (GameObject.Find("HellSceneTeleporterConvo(Clone)") == false)
    102.                         {
    103.                             Quaternion hitQuaternion = transform.rotation;
    104.                             //Create the prefab
    105.                             Instantiate(Resources.Load("HellSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    106.                                 hit.transform.position.y + 20, hit.transform.position.z + 20), hitQuaternion);
    107.  
    108.                             //Create the Prefab
    109.                             //Instantiate(Resources.Load("HellSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    110.                         }
    111.                     }
    112.                     if (hit.transform.name == "Winter Scene Teleporter")
    113.                     {
    114.                         //If there is already one created then dont create one
    115.                         if (GameObject.Find("WinterSceneTeleporterConvo(Clone)") == false)
    116.                         {
    117.                             Quaternion hitQuaternion = transform.rotation;
    118.                             //Create the prefab
    119.                             Instantiate(Resources.Load("WinterSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    120.                                 hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
    121.  
    122.                             //Create the Prefab
    123.                             //Instantiate(Resources.Load("WinterSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    124.                         }
    125.                     }
    126.                     if (hit.transform.name == "Test Scene Teleporter")
    127.                     {
    128.                         //If there is already one created then dont create one
    129.                         if (GameObject.Find("TeleporterConvo(Clone)") == false)
    130.                         {
    131.                             Quaternion hitQuaternion = transform.rotation;
    132.                             //Create the prefab
    133.                             Instantiate(Resources.Load("TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    134.                                 hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
    135.  
    136.                             //Create the Prefab
    137.                             //Instantiate(Resources.Load("TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    138.                         }
    139.                     }
    140.                     if (hit.transform.name == "Floor 1 Teleporter")
    141.                     {
    142.                         //If there is already one created then dont create one
    143.                         if (GameObject.Find("Floor1TeleporterConvo(Clone)") == false)
    144.                         {
    145.                             Quaternion hitQuaternion = transform.rotation;
    146.                             //Create the prefab
    147.                             Instantiate(Resources.Load("Floor1TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    148.                                 hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
    149.  
    150.                             //Create the Prefab
    151.                             //Instantiate(Resources.Load("Floor1TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    152.                         }
    153.                     }
    154.  
    155.  
    156.                 }
    157.  
    158.                 if (hit.transform.tag == "Mob")
    159.                 {
    160.                     PV.RPC("RpcSetTargetMob", RpcTarget.AllBuffered);
    161.  
    162.                     print("You clicked on " + transformOfHit.name);
    163.                 }
    164.  
    165.              
    166.             }
    167.         }
    168.     }
    169.  
    170.     [PunRPC]
    171.     void RpcSetTargetMob()
    172.     {
    173.         playerScript.targetMob = transformOfHit;
    174.     }
    175.  
    176.     [PunRPC]
    177.     void RpcSetTransformOfHit()
    178.     {
    179.         transformOfHit = hit.transform;
    180.     }
    181.  
    182.     [PunRPC]
    183.     void RpcSetHit()
    184.     {
    185.         Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    186.         Debug.Log("This ray hit assigned in some1 elses raycaster");
    187.     }
    188. }
     
  2. Tyasu

    Tyasu

    Joined:
    Feb 19, 2019
    Posts:
    3
     
  3. Tyasu

    Tyasu

    Joined:
    Feb 19, 2019
    Posts:
    3
    So I'm still not sure what it was that was preventing me from passing a reference from the clients RaycastHit directly but I was able to workaround this by referencing the PhotonId of the RaycastHit object (targeted enemy) on the Clients computer and passing that with OnPhotonSerializeView as an int then comparing that id to get the object for the rpc. This was a very complicated issue that took me 3 days to find a workaround for. I will post the code if any1 wants to learn how to find targeted objects from clients using a referenced PhotonId.

    Code (CSharp):
    1. public class Raycaster : MonoBehaviourPunCallbacks, IPunObservable
    2. {
    3.     public PhotonView PV;
    4.  
    5.     private Transform camRotate;
    6.  
    7.     float currentMousePosition;
    8.     float pastMousePosition;
    9.  
    10.     float screenWidth;
    11.     float screenHeight;
    12.  
    13.     public int idOfTarget = -1;
    14.  
    15.     public Camera cam;
    16.     public Player playerScript;
    17.     public Player targetPlayerScript;
    18.     public GameObject targetEnemyObject;
    19.  
    20.     public Transform transformOfHit;
    21.  
    22.     public RaycastHit targetHit;
    23.  
    24.     public Enemy targetEnemyScript;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         PV = GetComponent<PhotonView>();
    30.         playerScript = GetComponent<Player>();
    31.         screenWidth = Screen.width;
    32.         screenHeight = Screen.height;
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         if (!PV.IsMine)
    39.         {
    40.             return;
    41.         }
    42.  
    43.         if (cam == null)
    44.         {
    45.             cam = GetComponentInChildren<Camera>();
    46.         }
    47.  
    48.  
    49.         if (Input.GetMouseButtonDown(0))
    50.         {
    51.  
    52.             // if left button pressed...
    53.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    54.  
    55.             RaycastHit hit;
    56.  
    57.             if (Physics.Raycast(ray, out hit))
    58.             {
    59.                 targetHit = hit;
    60.  
    61.                 //Set transforOfHit to whatever the transform of the object that was hit
    62.                 transformOfHit = hit.transform;
    63.  
    64.                 if (transformOfHit != null)
    65.                 {
    66.                     if (transformOfHit.gameObject.GetPhotonView() != null)
    67.                     {
    68.                         idOfTarget = transformOfHit.gameObject.GetPhotonView().InstantiationId;
    69.                     }
    70.  
    71.                 }
    72.  
    73.  
    74.  
    75.  
    76.  
    77.                 // the object identified by hit.transform was clicked
    78.                 // do whatever you want
    79.                 if (hit.transform.tag == "NPC")
    80.                 {
    81.                     print("You clicked an NPC");
    82.  
    83.                     //If there is already one created then dont create one
    84.                     if (GameObject.Find("ConvoBox(Clone)") == false)
    85.                     {
    86.                         //Flips the convobox after instantiation
    87.                         Quaternion hitQuaternion = hit.transform.rotation * Quaternion.Euler(0, 180f, 0);
    88.                         //Create the prefab
    89.                         Instantiate(Resources.Load("ConvoBox"), new Vector3(hit.transform.position.x,
    90.                             hit.transform.position.y + 5, hit.transform.position.z + 5), hitQuaternion);
    91.                     }
    92.                     else
    93.                         Destroy(GameObject.Find("ConvoBox(Clone)"));
    94.                 }
    95.  
    96.                 if (hit.transform.tag == "Teleporter")
    97.                 {
    98.                     print("You clicked a Teleporter");
    99.  
    100.                     if (hit.transform.name == "Flower Scene Teleporter")
    101.                     {
    102.                         //If there is already one created then dont create one
    103.                         if (GameObject.Find("FlowerSceneTeleporterConvo(Clone)") == false)
    104.                         {
    105.  
    106.                             Quaternion hitQuaternion = transform.rotation;
    107.                             //Create the prefab
    108.                             Instantiate(Resources.Load("FlowerSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    109.                                 hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
    110.  
    111.                             //Create the Prefab
    112.                             //Instantiate(Resources.Load(""), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    113.                         }
    114.                     }
    115.                     if (hit.transform.name == "Hell Scene Teleporter")
    116.                     {
    117.                         //If there is already one created then dont create one
    118.                         if (GameObject.Find("HellSceneTeleporterConvo(Clone)") == false)
    119.                         {
    120.                             Quaternion hitQuaternion = transform.rotation;
    121.                             //Create the prefab
    122.                             Instantiate(Resources.Load("HellSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    123.                                 hit.transform.position.y + 20, hit.transform.position.z + 20), hitQuaternion);
    124.  
    125.                             //Create the Prefab
    126.                             //Instantiate(Resources.Load("HellSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    127.                         }
    128.                     }
    129.                     if (hit.transform.name == "Winter Scene Teleporter")
    130.                     {
    131.                         //If there is already one created then dont create one
    132.                         if (GameObject.Find("WinterSceneTeleporterConvo(Clone)") == false)
    133.                         {
    134.                             Quaternion hitQuaternion = transform.rotation;
    135.                             //Create the prefab
    136.                             Instantiate(Resources.Load("WinterSceneTeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    137.                                 hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
    138.  
    139.                             //Create the Prefab
    140.                             //Instantiate(Resources.Load("WinterSceneTeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    141.                         }
    142.                     }
    143.                     if (hit.transform.name == "Test Scene Teleporter")
    144.                     {
    145.                         //If there is already one created then dont create one
    146.                         if (GameObject.Find("TeleporterConvo(Clone)") == false)
    147.                         {
    148.                             Quaternion hitQuaternion = transform.rotation;
    149.                             //Create the prefab
    150.                             Instantiate(Resources.Load("TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    151.                                 hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
    152.  
    153.                             //Create the Prefab
    154.                             //Instantiate(Resources.Load("TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    155.                         }
    156.                     }
    157.                     if (hit.transform.name == "Floor 1 Teleporter")
    158.                     {
    159.                         //If there is already one created then dont create one
    160.                         if (GameObject.Find("Floor1TeleporterConvo(Clone)") == false)
    161.                         {
    162.                             Quaternion hitQuaternion = transform.rotation;
    163.                             //Create the prefab
    164.                             Instantiate(Resources.Load("Floor1TeleporterConvo"), new Vector3(hit.transform.position.x + 4,
    165.                                 hit.transform.position.y - 20, hit.transform.position.z + 4), hitQuaternion);
    166.  
    167.                             //Create the Prefab
    168.                             //Instantiate(Resources.Load("Floor1TeleporterConvo"), hit.point - new Vector3(7, 0, 0), Quaternion.identity);
    169.                         }
    170.                     }
    171.  
    172.  
    173.                 }
    174.  
    175.  
    176.             }
    177.  
    178.  
    179.         }
    180.  
    181.         PV.RPC("RpcSetTarget", RpcTarget.AllBuffered);
    182.     }
    183.  
    184.     void FixedUpdate()
    185.     {
    186.         if (idOfTarget != -1)
    187.         {
    188.             targetEnemyObject = PhotonView.Find(idOfTarget).gameObject;
    189.         }
    190.  
    191.     }
    192.  
    193.     [PunRPC]
    194.     void RpcSetTarget()
    195.     {
    196.         if (targetEnemyObject != null)
    197.         {
    198.             if (targetEnemyObject.transform.tag == "Mob")
    199.             {
    200.                 playerScript.targetMob = targetEnemyObject.transform;
    201.  
    202.                 print("You clicked on " + targetEnemyObject.name);
    203.  
    204.                 targetEnemyScript = playerScript.targetMob.gameObject.GetComponent<Enemy>();
    205.  
    206.                 if (targetEnemyScript.health <= 0)
    207.                 {
    208.                     idOfTarget = -1;
    209.                 }
    210.             }
    211.             else if (targetEnemyObject.transform.tag == "Player")
    212.             {
    213.                 playerScript.targetMob = targetEnemyObject.transform;
    214.                 targetPlayerScript = targetEnemyObject.GetComponent<Player>();
    215.                
    216.             }
    217.         }
    218.     }
    219.  
    220.     public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    221.     {
    222.         if (stream.IsWriting)
    223.         {
    224.             stream.SendNext(idOfTarget);
    225.         }
    226.         else
    227.         {
    228.             idOfTarget = (int)stream.ReceiveNext();
    229.         }
    230.     }
    231. }