Search Unity

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

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

  1. Mashimaro7

    Mashimaro7

    Joined:
    Apr 10, 2020
    Posts:
    727
    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.  
    11. public class Waepons : MonoBehaviourPunCallbacks
    12. {
    13.  
    14.     [SerializeField] Camera FPSCamera;
    15.     public LayerMask canBeShot;
    16.     [SerializeField] float damage = 20f;
    17.     [SerializeField] ParticleSystem effect;
    18.     [SerializeField] GameObject hitEffect;
    19.     [SerializeField] Munition munitionTyp;
    20.     [SerializeField] AmmoType ammoType;
    21.     [SerializeField] float shotZeit = 0.5f;
    22.     [SerializeField] TextMeshProUGUI monitionText;
    23.     [SerializeField] Text txt;
    24.     int ammoint;
    25.     bool canShot = true;
    26.  
    27.     private void Awake()
    28.     {
    29.         canShot = true;
    30.     }
    31.  
    32.     void Update()
    33.     {
    34.         Scene currentScene = SceneManager.GetActiveScene ();
    35.          // Retrieve the name of this scene.
    36.         string sceneName = currentScene.name;
    37.         if (!photonView.IsMine) { return; }
    38.        DisplayMunition();
    39.  
    40.         if (Input.GetMouseButtonDown(0) && canShot == true)
    41.         {
    42.             int.TryParse(txt.text, out ammoint);
    43.             if (ammoint >0){
    44.                 if (sceneName == "SinglePlayerMap"){
    45.                     munitionTyp.MounitionDown(ammoType);
    46.                 }
    47.                 photonView.RPC("Shoot", RpcTarget.All);
    48.             }
    49.  
    50.  
    51.         }
    52.     }
    53.  
    54.     public void DisplayMunition()
    55.     {
    56.        int currentAmmo = munitionTyp.AnzahlMounition(ammoType);
    57.        txt.text = currentAmmo.ToString();
    58.     //    if (Input.GetMouseButtonDown(0) && canShot == true)
    59.     //     {
    60.     //         currentAmmo -=1;
    61.          
    62.     //     }
    63.     //    txt.text = currentAmmo.ToString();
    64.      
    65.     }
    66.  
    67.     [PunRPC]
    68.     private void Shoot()
    69.     {
    70.      
    71.         canShot = false;
    72.         if (munitionTyp.AnzahlMounition(ammoType) > 0)
    73.         {
    74.             PlayEffect();
    75.             RayCast();
    76.             munitionTyp.MounitionDown(ammoType);
    77.         }
    78.      
    79.          //yield return new WaitForSeconds(shotZeit);
    80.          canShot = true;
    81.  
    82.     }
    83.  
    84.     //[PunRPC]
    85.     //private void Couroutine()
    86.     //{
    87.     //    StartCoroutine(Shoot());
    88.     //}
    89.  
    90.     private void PlayEffect()
    91.     {
    92.         effect.Play();
    93.     }
    94.  
    95.     private void RayCast()
    96.     {
    97.         RaycastHit hit;
    98.  
    99.         if (Physics.Raycast(FPSCamera.ViewportPointToRay(new Vector3(0.5f, 0.5f)), out hit))
    100.         {
    101.  
    102.             CreatHitImpact(hit);
    103.             Health_Player target = hit.transform.GetComponent<Health_Player>();
    104.             if (target == null) return;
    105.  
    106.             hit.collider.gameObject.GetPhotonView().RPC("PlayerDamage", RpcTarget.All, damage);
    107.  
    108.  
    109.         }
    110.  
    111.         else
    112.         {
    113.             return;
    114.         }
    115.     }
    116.  
    117.     private void CreatHitImpact(RaycastHit hit)
    118.     {
    119.        GameObject impact = Instantiate(hitEffect, hit.point, Quaternion.LookRotation(hit.normal));
    120.         Destroy(impact, 0.1f);
    121.     }
    122.  
    123.     [PunRPC]
    124.     private void PlayerDamage(float damage)
    125.     {
    126.         if (photonView.IsMine) return;      
    127.             GetComponent<Health_Player>().PlayerDamage(damage);
    128.     }
    129. }
    130.  
    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 tried everything.
     
    Last edited: Apr 11, 2021