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

Player hit detection problems

Discussion in 'Multiplayer' started by macdonaghpm, Aug 27, 2021.

  1. macdonaghpm

    macdonaghpm

    Joined:
    Apr 26, 2021
    Posts:
    63
    I am making a multiplayer fps but I have a bug. In the start the hit detection works. When you get 5 hits you get a kill. But after that the hit detection works only some of the time, strangely enough it seems like the amount of hits you take is always a multiple of 5 implying that every hit takes just as long, but it is hard to test so I am not sure. It seems to be that this glitch only occurs when one person has killed, and if only one person kills the whole match it works, but again it is hard to test. I used Debug.Log with the health value on a hit to test.

    Here is my code for killing the player:

    Code (CSharp):
    1.  
    2. [PunRPC]
    3.         public void Die(int killer, int killed, PhotonMessageInfo info)
    4.         {
    5.             //rb.enabled = false;
    6.             if(killer == photonView.ViewID && Health > 0)
    7.             {
    8.                 Debug.Log("I killed");
    9.             }
    10.             if (controllable == true && Health <= 0)
    11.             {
    12.                 Health = 5;
    13.                 controllable = false;
    14.                 rb.velocity = Vector3.zero;
    15.                 rb.angularVelocity = Vector3.zero;
    16.                 object lives;
    17.                 //Cursor.lockState = CursorLockMode.None;
    18.                 //Cursor.visible = true;
    19.                 //SecondCamera.SetActive(false);
    20.                 if (PhotonNetwork.LocalPlayer.CustomProperties.TryGetValue(Game.PLAYER_LIVES, out lives))
    21.                 {
    22.                     PhotonNetwork.LocalPlayer.SetCustomProperties(new Hashtable {{Game.PLAYER_LIVES, ((int) lives <= 1) ? 0 : ((int) lives - 1)}});
    23.                     StartCoroutine("WaitForRespawn");
    24.                     if (((int) lives) > 1)
    25.                     {
    26.                         StartCoroutine("WaitForRespawn");
    27.                     }
    28.                 }
    29.             }
    30.         }
    Here is the fire procedure:

    Code (CSharp):
    1.  
    2. [PunRPC]
    3.         public void Fire(Vector3 posit,Vector3 rotat, int hitter, PhotonMessageInfo info)
    4.         {
    5.             float lag = (float) (PhotonNetwork.Time - info.SentServerTime);
    6.             RaycastHit hit;
    7.             /** Use this if you want to fire one bullet at a time **/
    8.             Physics.Raycast(posit, rotat, out hit);
    9.             if(hit.collider)
    10.             {
    11.                 //" && hitter != photonView.ViewID ,&& hitter != pv
    12.                 if(hit.collider.tag == "me")
    13.                 {
    14.                     Health--;
    15.                     Debug.Log(Health);
    16.                     if(Health <= 0)
    17.                     {
    18.                         photonView.RPC("Die", RpcTarget.AllViaServer, hitter,photonView.ViewID);
    19.                     }
    20.                 }
    21.                 else if(hit.collider.tag == "Player")
    22.                 {
    23.                     Instantiate(blood,hit.point,Quaternion.Euler(-rotat));
    24.                 }
    25.                 else{
    26.                     Instantiate(impact,hit.point,Quaternion.Euler(-rotat));
    27.                 }
    28.             }
    29.         }
    Here is where the fire procedure is called:

    Code (CSharp):
    1.  
    2. if (Input.GetButtonDown("Fire1")&& shootingTimer <= 0.0)
    3.             {
    4.                 shootingTimer = 0.2f;
    5.                 ray = Cam.ViewportPointToRay(new Vector3(0.5f, 0.5f));
    6.                 ray.origin = Cam.transform.position;
    7.                 photonView.RPC("Fire", RpcTarget.AllViaServer, Gun.transform.position, Cam.transform.TransformDirection(Vector3.forward), photonView.ViewID);
    8.             }
     
  2. macdonaghpm

    macdonaghpm

    Joined:
    Apr 26, 2021
    Posts:
    63
    I think I may be able to check hits on the client that shot and then say they got hit. I will keep the fire code but all it will do is impacts. I don't know if this will work but I could try it.
     
  3. macdonaghpm

    macdonaghpm

    Joined:
    Apr 26, 2021
    Posts:
    63
    I think I fixed it
     
    tobiass likes this.