Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Whitby93 · Jun 06, 2016 at 10:34 PM · networkinginstantiateunity5client-server

UNET - Syncing prefabs spawned by clients.

When a client fires it spawns a number of prefabs which then kill themselves shortly afterwards. They contain a number of feedback mechanics including sound and particle effects, none of which need to be synced other than when/where the prefab is instantiated.

I've added these prefabs to the spawnable objects list and they work on the client's side, but not on the server. (Or they work on the server but not on the client.)

I've messed around with Network Identities and so forth to no avail; what is the correct way of telling the server a prefab has been spawned, where it has been spawned and which direction it is facing?

(Much of my difficulty has come from building a game in JS and the few networking tutorials all seem to be in C#. My currently functional networking for player movement is all in C# and handled in different scripts. If possible, I would appreciate help understanding how to use UNET correctly in JS, although if anybody has a C# solution to my current problem which can run as a standalone script on the client-instantiated prefab, I will be elated. I have no experience with C#, but a solution is a solution.)

Comment
Add comment · Show 1
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 melwei · Jun 07, 2016 at 04:43 PM 0
Share

I have no experience with JS yet but maybe I can help you out with a c# solution. Is your problem, that the bullets (etc.) only spawn at the client's or the server's game?

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by melwei · Jun 08, 2016 at 12:00 PM

You should use NetworkView.RPC (c#, maybe the same for JS) to use functions on both server and client.

First add a NetworkView component to your object. Then create an RPC function, which Instantiates your Object:

 [RPC]
 void MyRPCFunction( myArgs ){
    //Do Stuff, e.g. Set Player Settings
 }

Create an RPC call, whenever you want to call the RPC function:

 NetworkView MyNW=gameObject.GetComponent<NetworkView>;
 MyNW.RPC("MyRPCFunction",RPCMode.All,myArgs);

Comment
Add comment · Show 1 · 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
avatar image EpiFouloux · Jun 08, 2016 at 02:41 PM 1
Share

Now UNet uses

 [ClientRPC]
 private void Rpc_function()
 {
 
 }

RPC is deprecated

avatar image
0

Answer by Whitby93 · Jun 07, 2016 at 11:06 PM

The prefabs only instantiate in the game world of the client that instantiated them.

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
avatar image
0

Answer by EpiFouloux · Jun 08, 2016 at 04:19 PM

Acutally that's how your spawning should work:

 private void fire()
 {
   Vector3 direction = transform.forward;
   Cmd_fire(direction)
 }
 
 [Command]
 private void Cmd_fire(Vector3 direction)
 {
   GameObject bullet = (GameObject) Instantiate(bullet_prefab, transform.position, Quaternion.identity);
 bullet_script = bullet.getComponent<bullet_script>();
 
 bullet_script.direction = direction;
 NetworkServer.Spawn(bullet);
 }

Your bullet prefab must have a NetworkIdentity component and it script should be like this:

 public class bullet_script : NetworkBehaviour
 {
 [SyncVar] public Vector3 direction;
 
 void Start()
 {
 // rotate your bullet
 }
 
 void Update()
 {
 // do your stuff
 }
 }
Comment
Add comment · Show 8 · 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
avatar image EpiFouloux · Jun 08, 2016 at 02:51 PM 0
Share

I'm sorry I don't have a JS solution, but I think it's quite easy to adapt.. hope it helps !

avatar image Whitby93 · Jun 13, 2016 at 01:01 AM 0
Share

Okay, I've got something and it did something.

Current situation:

Crash-free code, no errors.

Prefabs spawned on the server/client are visible on other clients.

Prefabs spawned on clients are not visible on server.

Prefab rotation is correct in the (host) game instance they are spawned, but their rotation is incorrect (on the clients).

Help requested:

The prefabs do not need to be rotated/updated once they are spawned in. Is this not possible to do in the instantiation script since their location does not need continuously updating?

The Network$$anonymous$$anager on each prefab. Where should the checkbox be? Client? Server? Neither?

Snippet of code from main script instantiating prefabs:

 #pragma strict
 import UnityEngine.Networking;
 
 var direction  : Vector3;
 
 
 class IAmACannon extends NetworkBehaviour {
 
 function InstantiateFiringPrefab () {
  {
    direction = transform.forward;
    StartCoroutine("Cmd_fire");    
  }
 }
 
 @Command
 function Cmd_fire()
     {
         var cannonEffects = Instantiate(aGunFiring, transform.position + offsetPos, transform.rotation);
         var cannonSync = cannonEffects.GetComponent(CannonSync);
 
 
         
 //        cannonSync.direction = direction;
         NetworkServer.Spawn(cannonEffects);
     }
 }

Code applied to the prefab. Not sure what I'm doing with this...

 #pragma strict
 import UnityEngine.Networking;
 class CannonSync extends NetworkBehaviour {
 
 @SyncVar var direction : Vector3;
 @SyncVar var rotation : Vector3;
 
 @SerializeField var myTransform : Transform;
 
 function Start(){
     StartCoroutine("TransmitPosition");
 }
 
     @Command
         function CmdProvidePositionToServer (Vector3){
         }
 
     @ClientCallback
         function TransmitPosition (){
             if (isLocalPlayer) {
                 CmdProvidePositionToServer (myTransform.position);
 //                This isn't being called. If it was, it would cause a crash since it's C# in a JS file.
             }
         }
 }



avatar image EpiFouloux Whitby93 · Jun 13, 2016 at 07:31 AM 0
Share

I think you can just put a NetworkTransform component on your prefab, and set the send interval to 0 (so it will be synchronized once)

avatar image Whitby93 EpiFouloux · Jun 13, 2016 at 03:53 PM 0
Share

The NetworkTransform worked perfectly, not sure why I didn't think of that myself.

I still have the issue that the prefabs don't spawn on the server if the client fires though... whereabouts is the problem in my code/what am I missing that is causing this?

Show more comments

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

7 People are following this question.

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

Related Questions

Unet NetworkServer.Spawn() not working 5 Answers

Networking already instantiated assets 1 Answer

Unet: Client/Server end up with two different animations 0 Answers

ClientRpc Function not being carried out on all clients in Unity3d c# 0 Answers

Networked Objects not appearing on Client when Client loads scene first 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