Search Unity

Third Party Photon, Player who Shoots Takes Damage, but not Player who is Shot

Discussion in 'Multiplayer' started by Mashimaro7, Apr 11, 2021.

  1. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    I posted this in scripting but just remembered this is a multiplayer issue lol

    I'm trying to get this online FPS working, for some reason, whenever I shoot another player, the shooter takes damage, not the... shootee lol.

    Here's the shooting script,

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using TMPro;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7. using Photon.Pun;
    8. using UnityStandardAssets.Characters.FirstPerson;
    9. using UnityEngine.SceneManagement;
    10. public class Waepons : MonoBehaviourPunCallbacks
    11. {
    12.     [SerializeField] Camera FPSCamera;
    13.     public LayerMask canBeShot;
    14.     [SerializeField] float damage = 20f;
    15.     [SerializeField] ParticleSystem effect;
    16.     [SerializeField] GameObject hitEffect;
    17.     [SerializeField] Munition munitionTyp;
    18.     [SerializeField] AmmoType ammoType;
    19.     [SerializeField] float shotZeit = 0.5f;
    20.     [SerializeField] TextMeshProUGUI monitionText;
    21.     [SerializeField] Text txt;
    22.     int ammoint;
    23.     bool canShot = true;
    24.     private void Awake()
    25.     {
    26.         canShot = true;
    27.     }
    28.     void Update()
    29.     {
    30.         Scene currentScene = SceneManager.GetActiveScene ();
    31.          // Retrieve the name of this scene.
    32.         string sceneName = currentScene.name;
    33.         if (!photonView.IsMine) { return; }
    34.        DisplayMunition();
    35.         if (Input.GetMouseButtonDown(0) && canShot == true)
    36.         {
    37.             int.TryParse(txt.text, out ammoint);
    38.             if (ammoint >0){
    39.                 if (sceneName == "SinglePlayerMap"){
    40.                     munitionTyp.MounitionDown(ammoType);
    41.                 }
    42.                 photonView.RPC("Shoot", RpcTarget.All);
    43.             }
    44.         }
    45.     }
    46.     public void DisplayMunition()
    47.     {
    48.        int currentAmmo = munitionTyp.AnzahlMounition(ammoType);
    49.        txt.text = currentAmmo.ToString();
    50.     //    if (Input.GetMouseButtonDown(0) && canShot == true)
    51.     //     {
    52.     //         currentAmmo -=1;
    53.        
    54.     //     }
    55.     //    txt.text = currentAmmo.ToString();
    56.    
    57.     }
    58.     [PunRPC]
    59.     private void Shoot()
    60.     {
    61.    
    62.         canShot = false;
    63.         if (munitionTyp.AnzahlMounition(ammoType) > 0)
    64.         {
    65.             PlayEffect();
    66.             RayCast();
    67.             munitionTyp.MounitionDown(ammoType);
    68.         }
    69.    
    70.          //yield return new WaitForSeconds(shotZeit);
    71.          canShot = true;
    72.     }
    73.     //[PunRPC]
    74.     //private void Couroutine()
    75.     //{
    76.     //    StartCoroutine(Shoot());
    77.     //}
    78.     private void PlayEffect()
    79.     {
    80.         effect.Play();
    81.     }
    82.     private void RayCast()
    83.     {
    84.         RaycastHit hit;
    85.         if (Physics.Raycast(FPSCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f)), out hit))
    86.         {
    87.             CreatHitImpact(hit);
    88.             Health_Player target = hit.transform.GetComponent<Health_Player>();
    89.             if (target == null) return;
    90.             hit.collider.gameObject.GetPhotonView().RPC("PlayerDamage", RpcTarget.All, damage);
    91.         }
    92.         else
    93.         {
    94.             return;
    95.         }
    96.     }
    97.     private void CreatHitImpact(RaycastHit hit)
    98.     {
    99.        GameObject impact = Instantiate(hitEffect, hit.point, Quaternion.LookRotation(hit.normal));
    100.         Destroy(impact, 0.1f);
    101.     }
    102.     [PunRPC]
    103.     private void PlayerDamage(float damage)
    104.     {
    105.         if (photonView.IsMine) return;    
    106.             GetComponent<Health_Player>().PlayerDamage(damage);
    107.     }
    108. }
    109.  
    Don't mind the messiness, this is the clients script lol. But anyway, I've even tried inverting the if statement in the player damage method, it does the same thing...

    Here's a video. Warning, gun shots are loud lol



    Does anyone know how to fix this? I've been trying for hours...

    Edit: I solved it... My client made this extremely confusing, the script I posted above was being used, but a separate script was ALSO raycasting and actually causing damage to yet another script lol. I was confused because I was calling prints to my above script and it was printing them off, but when I disabled the whole raycast, everything still functioned :/
     
    Last edited: Apr 12, 2021