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

Syncing Gun sounds?

Discussion in 'Multiplayer' started by AtomicCabbage33, Jan 23, 2016.

  1. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I have tried to make a script which syncs gun sounds so when player A shoots, player B hears the gun fire from player A's audio source. I have tried to use RPC's but got nowhere. If someone could write a quick script which achieves this or link me to a script that would be brilliant. FYI, I'm using uNet.
     
  2. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    681
    You'll be hard pressed to get someone to write a script for you around here...even one so simple. However, you're on the right track. You can use a ClientRPC, AND a Command. The player shooting would send a Command to the host/server that would then send a ClientRPC out to all clients to make that player's PlayerObject execute the sound.
     
  3. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    I've written this but I get the error '
    Uncaught exception in async net callback: Object reference not set to an instance of an object
    UnityEditor.AsyncHTTPClient:Done(State, Int32)' and I cannot hear anything. Any ideas?

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class PlayerShoot : NetworkBehaviour {
    5.  
    6.     private const string PLAYER_TAG = "Player";
    7.  
    8.     public PlayerWeapon weapon;
    9.  
    10.     public GameObject hitPar;
    11.  
    12.     public GameObject hitPar_1;
    13.  
    14.     private AudioSource audioSource;
    15.  
    16.     public AudioClip hurtSound;
    17.  
    18.     [SyncVar]
    19.     public AudioClip shootSound;
    20.  
    21.     [SerializeField]
    22.     private Camera cam;
    23.  
    24.     [SerializeField]
    25.     private LayerMask mask;
    26.  
    27.     void Awake ()
    28.     {
    29.         audioSource = GetComponent<AudioSource> ();
    30.     }
    31.  
    32.     void Start ()
    33.     {
    34.         if (cam == null)
    35.         {
    36.             Debug.LogError("PlayerShoot: No camera referenced!");
    37.             this.enabled = false;
    38.         }
    39.     }
    40.  
    41.     void Update ()
    42.     {
    43.         if (Input.GetButtonDown("Fire1"))
    44.         {
    45.             Shoot();
    46.             PlaySound ();
    47.         }
    48.     }
    49.  
    50.     [Client]
    51.     void Shoot ()
    52.     {
    53.         RaycastHit _hit;
    54.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask) )
    55.         {
    56.             if (_hit.collider.tag == PLAYER_TAG)
    57.             {
    58.                 CmdPlayerShot (_hit.collider.name, weapon.damage);
    59.                 GameObject par_clone = Instantiate (hitPar_1, _hit.point, Quaternion.LookRotation (_hit.normal)) as GameObject;
    60.                 Destroy (par_clone, 0.5f);;
    61.                 audioSource.clip = hurtSound;
    62.                 audioSource.Play ();
    63.             }
    64.             else
    65.             {
    66.                 GameObject par_clone = Instantiate (hitPar, _hit.point, Quaternion.LookRotation (_hit.normal)) as GameObject;
    67.                 Destroy (par_clone, 0.5f);;
    68.             }
    69.  
    70.         }
    71.  
    72.     }
    73.  
    74.     [Command]
    75.     void CmdPlayerShot (string _playerID, int _damage)
    76.     {
    77.         Debug.Log(_playerID + " has been shot.");
    78.         Player _player = GameManager.GetPlayer(_playerID);
    79.         _player.RpcTakeDamage(_damage);
    80.     }
    81.        
    82.     public void PlaySound()
    83.     {
    84.         if(!isServer)
    85.         {
    86.             return;
    87.  
    88.         RpcSound ();
    89.         }
    90.  
    91.     }
    92.        
    93.  
    94.     [ClientRpc]
    95.  
    96.     void RpcSound()
    97.     {
    98.         if(!isLocalPlayer)
    99.         {
    100.             audioSource.clip = shootSound;
    101.             audioSource.Play ();
    102.         }
    103.     }
    104. }
     
  4. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    681
  5. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
  6. DRRosen3

    DRRosen3

    Joined:
    Jan 30, 2014
    Posts:
    681
    lol Sorry...I'm not familiar with that error you got so I was hoping you could find an answer to the error in that thread.
     
  7. AtomicCabbage33

    AtomicCabbage33

    Joined:
    Aug 31, 2015
    Posts:
    141
    No worries, I found out the cause anyway :)
     
    DRRosen3 likes this.
  8. Shabby91

    Shabby91

    Joined:
    Apr 20, 2017
    Posts:
    1

    Can you share your script if its working ?