Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cj31387 · Oct 04, 2013 at 12:04 PM · fpsnetworksoundrpc

How to Network Sound?

EDIT: Does anyone know of any mp unity open source projects that have mp sound that I could check out? Kinda needing some reference.

Right now I'm making a multiplayer death-match game. So far I have a lot of stuff networked like damage, respawing. But I'm having a problem with sound. Right now I have a prefab player that spawns into the game when he joins a server. Everyone uses this same prefab and it clones. What I'm trying to do is when a player shoots his gun a sound rpc goes to the other players. I'm not quite sure how to set this up though. Because they both have the same gameobject that controls their sounds. When A shoots, B hears HIS selected gun and HIS soundsource holder. But I want A to shoot and B to hear it from A's sound source, not B. Currently when A shoots, B hears the shot from B. Not A. How would I set up the rpc to make the sound ON the person that is shooting so the other player can hear that sound coming from where the other person is shooting.

Here is my code so far.

 using UnityEngine;
 using System.Collections;
 
 public class GunfireSFX : MonoBehaviour
 {
     public AudioSource aS;
     public AudioClip pistolShot;
     public AudioClip revolverShot;
 
     public GameObject pistolActive;
     public GameObject revolverActive;
 
 
     // Detemine which gun is equiped and change the sound source to that
 
     void Awake()
     {
 
     }
 
     void Update()
     {
         CheckEquipedGun();
         GunFireSFXCallRPC();
     }
 
     public void CheckEquipedGun()
     {
         if (networkView.isMine)
         {
             if (pistolActive.activeInHierarchy == true)
             {
                 aS.clip = pistolShot;
             }
 
             if (revolverActive.activeInHierarchy == true)
             {
                 aS.clip = revolverShot;
             }
         }
     }
 
     public void GunFireSFXCallRPC()
     {
         if (Input.GetMouseButtonUp(0))
         {
             networkView.RPC("MPPlayFireSound", RPCMode.Others);
         }
     }
 
     // Play that soundsource as RPC.
     [RPC]
     public void MPPlayFireSound()
     {
         print(aS.clip.name);
         aS.Play();
         print("RPC Played GunFire");
     }
 }
 
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cj31387 · Oct 04, 2013 at 12:25 PM 0
Share

That just creates null references. I need to somehow send the RPC FRO$$anonymous$$ A, to B. But its just going to B's soundsource, thus the null reference if i have enabled false if its not $$anonymous$$e.

avatar image KiraSensei · Oct 04, 2013 at 12:28 PM 0
Share

Yes, of course you need to valuate the vars you will need on RPC calls before disabling the script :)

avatar image cj31387 · Oct 04, 2013 at 10:54 PM 0
Share

How do you do that?

avatar image cj31387 · Oct 05, 2013 at 03:06 AM 0
Share

and what do you mean by valuate the vars before disabling the script. if the vars are in the script and network view isn't $$anonymous$$e they instantly are null.

avatar image KiraSensei · Oct 05, 2013 at 09:24 AM 0
Share

In the awake method, you disable the script with :

 if (!networkView.is$$anonymous$$ine)
 {
     this.enabled = false;
     return;
 }

If you can't valuate the aS var before (like here because you need to know what weapon has the other player), you need to pass the information of what clip should be played into the RPC call. So maybe you can declare an enum containing all sorts of shots you can play : pistolShot, revolverShot, ... and then in the RPC call you pass an int corresponding to the shot you want to play. This way the other player knows what weapon the 1st player shoots with.

With this, the Update method is never called, because it is disabled, but the RPC calls are still made, and if enough information is passed threw the RPC call, it will work.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KiraSensei · Oct 04, 2013 at 12:19 PM

I'm doing the same on my game, but on the Awake() method I check if the network view is mine, if not, I disable the script. Try that for a start.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with identifying players in network 1 Answer

Problem with RPC life 0 Answers

Transfering vars to RPCs? 0 Answers

Playing Audio Over Network -1 Answers

Can not make RPC calls during Start() 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges