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 /
  • Help Room /
avatar image
5
Question by Glomby · Sep 18, 2016 at 06:16 PM · interactionmultiplayer networkingauthoritative

UNET: How do I properly handle Client Authority with interactable Objects?

I'm currently working on a VR game and one of the basic interactions should be the manipulation of objects. A player should be able to grab an object and throw it and another player should be able to catch it. For this to work the player should have authority over the object he holds/interacts with. To test this out I've created a very basic scene but ran into some problems. I can change PlayerAuthority sometimes but most of the time the player who is also the server (host) has authority.

As a quick and dirty method I'm using triggers to change authority. When a Local Player touches the object he gets authority and can interact with the object. When he leaves the trigger authority will be revoked. This is how the trigger on my test object looks like:

The controller of the local player has the tag "VRController" and the local player himself has the tag "VRLocalPlayer".

 void OnTriggerEnter(Collider other)
  {        
        if (other.gameObject.CompareTag("VRController"))
             {            
                 if (!isServer)
                 {   
                     GameObject player = GameObject.FindGameObjectWithTag("VRLocalPlayer");                
                     player.GetComponent<VRPlayerController>().CmdSetAuth(netId, player.GetComponent<NetworkIdentity>());
                 }            
             }              
         }
 
     void OnTriggerExit(Collider other) 
     {           
             if (other.gameObject.CompareTag("VRController"))
             {
                 if (!isServer)
                 {
                     GameObject player = GameObject.FindGameObjectWithTag("VRLocalPlayer");
                     player.GetComponent<VRPlayerController>().CmdRemAuth(netId, player.GetComponent<NetworkIdentity>());
                 }            
         }
   } 

On the actual Local Player Object are the Commands:

  [Command]
   public void CmdSetAuth(NetworkInstanceId objectId, NetworkIdentity player)
   {
                 var iObject = NetworkServer.FindLocalObject(objectId);
                 var networkIdentity = iObject.GetComponent<NetworkIdentity>();
                 networkIdentity.AssignClientAuthority(player.connectionToClient);               
    }
         
             
  [Command]
  public void CmdRemAuth(NetworkInstanceId objectId, NetworkIdentity player)
  {
                 var iObject = NetworkServer.FindLocalObject(objectId);
                 var networkIdentity = iObject.GetComponent<NetworkIdentity>();
                 networkIdentity.RemoveClientAuthority(player.connectionToClient);        
   }
 

This works sometimes but only under certain conditions. Most of the time a client can see what the Host does but not vice versa. With this script enabled the host is also able to see what another client does. But when the host touches an object adn puts it back down the client can pick it up on his end but the host won't be able to see it.

This is the first time I'm messing with Unet or multiplayer games in general, so I'm pretty sure I'm forgetting alot of things here.

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 Glomby · Sep 19, 2016 at 09:11 PM 0
Share

I implemented some changes today to make it all more robust and maybe even fix it.

I moved away from using trigger event and just use a function that activates when an object is actually grabbed. Now I can execute code when I grab an object and when I release it. When I grab it I send the command to assign authority to the player that is grabbing the object und when the player releases it I send the command to release it.

This doesn't change a whole lot other than it doesn't get triggered multiple times in a row. Just once when you grab and once when you release. This actually works better too even so it doesn't work yet. One player can grab an object und move it and the other player can grab an object and move it too. But when a player releases the object and trys to grab the object the other player was holding (but isn't holding anymore) he can't move it. It seems like the object is still connected to the other client.

The first AssignclientAuthority call seems to work as expected but the RemoveClientAuthority doesn't work at all or doesn't work in a way I'm expecting it to work. I have no idea what is causing this. The first half of what I'm trying to do works but not the secand half.

In short once an object is assigned to a player the player can move his object as much as he wants and the other player will see it. But when he puts the object down and the other player trys to move it, the object is locked in place or move a little bit and then snaps back into position. I'm not syncing the postion of any objects manually I'm just using the Transform Component.

avatar image Morderkaine · Nov 14, 2016 at 03:33 AM 0
Share

Hi, Have you found a solution to this? I have been having pretty much the same problem - client cannot pick up an object, or if they could the server wouldn't see what they were doing and it would snap back to the servers known position of it.

4 Replies

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

Answer by Glomby · Nov 16, 2016 at 05:24 PM

It's still not perfect but I have a working solution now.

Basically the way it works now is like this: Each object that is interactable has a script attached which contains an event listener. Whenever a player grabs an object this event will be called. This is how it looks like:

 private void OnGrab(object sender, InteractableObjectEventArgs e)
     {
         player = GameObject.FindGameObjectWithTag("VRLocalPlayer");
         playerID = player.GetComponent<NetworkIdentity>();
         player.GetComponent<VRPlayerController>().CmdSetAuth(netId, playerID);
     }

The local player object has a tag to identify it. This is what this script is looking for first. Then I need the netID of the player and the object. The player object has a player controller script attached and this is what I'm calling next.

 [Command]
     public void CmdSetAuth(NetworkInstanceId objectId, NetworkIdentity player)
     {
         var iObject = NetworkServer.FindLocalObject(objectId);
         var networkIdentity = iObject.GetComponent<NetworkIdentity>();
         var otherOwner = networkIdentity.clientAuthorityOwner;        
 
         if (otherOwner == player.connectionToClient)
         {
             return;
         }else
         {
             if (otherOwner != null)
             {
                 networkIdentity.RemoveClientAuthority(otherOwner);
             }
             networkIdentity.AssignClientAuthority(player.connectionToClient);
         }        
     }


This is where I manage the actual authorities. So whenever a player grabs an object this function checks if the player already has authority and does nothing. If the player doesn't have authority it checks if anyone else has authority and removes it and lastly gives the authority to the player who grabs the object.

This makes sure that only one player at a time has authority. It works pretty well but it could still need some refinements.

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 mr_pablo · May 12, 2017 at 04:30 PM

I am having the same issue, however, my spawned object is always classed as being the server (which i guess it is...)

But I need the object to be spawned by the server, but hand over authority?

One player can get authority, and even though it is released after interacting, the next player cannot get authority, despite requesting it, using your code.

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 angusmf · Jun 05, 2017 at 08:03 PM 0
Share

https://issuetracker.unity3d.com/issues/network-authority-problems

avatar image
0

Answer by jeck001 · May 31, 2017 at 07:18 AM

hi @Glomby are you able to resolve this?

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 inch · Apr 17, 2019 at 10:32 AM

Oh my god, thank you!!! works perfect!!!

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

73 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

Related Questions

pick up an object and give it an animation 0 Answers

Unity & real image? 0 Answers

Leap Motion Hand & Unity Interaction Engine - Why isn't my Move script working on ContactEnd() event? 0 Answers

Having problems with multiplayer syncing 0 Answers

Interactive unconscious person pointers? 0 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