• 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 Riiich · Sep 11, 2018 at 11:49 PM · networkingnetworkviewid

UNET object that anyone can move

So I'm making a simple block "editing" game. Each block has handles and I'm fully able to change the scale and change the position of the blocks. Now I want to add the blocks to multiplayer. I've gotten really close a few times but it seems to always come down to server authority.

My question is:

How do I spawn a block so everyone has authority to change the blocks transform values?


Please make a simple list with t$$anonymous$$ngs like:

  • prefab needs to be in the network manager, registered spawnable prefabs

  • prefab needs a Network Identity

  • Network Identity needs "Local Player Authority" checked

  • when you spawn an object use t$$anonymous$$s script...

  • when you edit the transform values, change them on all computers with...

    I've tried so many variations of the script from different tutorials like bullet spawning, enemy spawning but they are always "tied to" either the player or the server. Is there not a way for an object to be independent?


T$$anonymous$$s is my current script ObjectSync.cs:
using UnityEngine; using UnityEngine.Networking; public class ObjectSync : NetworkBehaviour { public bool beingUsed = false; [Command] public void CmdUpdateTransform(Vector3 _pos, Quaternion _rot, Vector3 _sca) { transform.position = _pos; transform.rotation = _rot; transform.localScale = _sca; RpcUpdateTransform (_pos, _rot, _sca); } [Command] public void CmdBeingUsed(bool _b) { beingUsed = _b; RpcBeingUsed (_b); } [ClientRpc] public void RpcUpdateTransform(Vector3 _pos, Quaternion _rot, Vector3 _sca) { transform.position = _pos; transform.rotation = _rot; transform.localScale = _sca; } [ClientRpc] public void RpcBeingUsed(bool _b) { beingUsed = _b; } }

And here is how I'm spawning the object:
[Command] void CmdSpawnObject(string go, Vector3 pos, Quaternion rot) { GameObject spawnedObject = (GameObject)Instantiate(cube, pos, rot); NetworkServer.Spawn(spawnedObject); }

The cubes are working on the server but are not spawning at all on the client

Comment
Add comment · Show 2
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 Riiich · Sep 12, 2018 at 01:55 AM 0
Share
avatar image Riiich · Sep 12, 2018 at 12:08 PM 0
Share

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Sep 12, 2018 at 12:04 AM

You simply can't give everyone authority over the same object. Note that every player has it's own environment and it's own version of every object. If the owner change the position of the object it sends t$$anonymous$$s change to the server w$$anonymous$$ch relays t$$anonymous$$s information to all other peers. However if two or more players could send updates it's almost impossible to tell whos position change should actually be the current position. Imagine player 1 move an object to (10,20,30) and player 2 moves it to (-45,10,100). What should actually happen? The object can only be in one position.


What you usually do in such cases is let the server own the object and use commands that the players can send over their playerobjects to request a certain change on that object

Comment
Add comment · Show 3 · 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 Riiich · Sep 12, 2018 at 12:11 AM 0
Share
avatar image Riiich Riiich · Sep 12, 2018 at 12:58 AM 0
Share
avatar image Bunny83 Riiich · Sep 13, 2018 at 11:26 AM 0
Share
avatar image
0

Answer by villevli · Sep 12, 2018 at 02:44 PM

Here's an example how t$$anonymous$$s could be implemented.


  • The block objects must be spawned and owned by the server. If you want the clients to be able to spawn them, add a Command for that.

  • The script with the Commands must be on the player object/prefab, not on the block object/prefab.

  • The block GameObjects must have a NetworkIdentity component and a NetworkTransform to sync the changes to clients. Instead of a NetworkTransform, you can also use your own ClientRpc to update the transform on the clients.


T$$anonymous$$s is the base of the script (attach t$$anonymous$$s to the player controller object of each player, single component per player):

 [SyncVar]
 GameObject editingBlock;
 // T$$anonymous$$s is a reference to the block currently being edited by t$$anonymous$$s player.
 // The server may set t$$anonymous$$s to the requested block through the CmdStartEditing command and it is automatically synced back to the clients.
 
 [Command]
 void CmdStartEditing(GameObject block)
 {
     // The server may update editingBlock here and the SyncVar will sync it's value back to the client.
     // If any other player has the same value (excluding null), t$$anonymous$$s command must fail. To check t$$anonymous$$s, you can maintain a list of players or fetch the PlayerControllers using the NetworkServer.connections api.
     // If the editingBlock does not change to the requested value, the client knows that the requested block cannot be edited. You can add a ClientRpc to notify the client about failure or success.
 }
 
 [Command]
 void CmdEndEditing()
 {
     // The server can set editingBlock to null here.
 }
 
 [Command]
 void CmdEditBlock(Vector3 position)
 {
     // The server sets values of the transform on the editingBlock object. Add the parameters you need.
     // The transform is synced to the clients using a NetworkTransform or a ClientRpc.
 }
Comment
Add comment · Show 2 · 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 Riiich · Sep 13, 2018 at 05:41 AM 0
Share
avatar image Bunny83 · Sep 13, 2018 at 11:38 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Convert Network.ViewID to Int 1 Answer

Best way to access player's gameobject across network 2 Answers

NetworkView is always mine, so can control all players 2 Answers

Network.Instantiate and NetworkServer.Spawn not working 2 Answers

Unity networking tutorial? 6 Answers


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