Search Unity

Getting HTC Vive Controllers to be Network Aware for ID

Discussion in 'Multiplayer' started by knighthawk, Jun 12, 2017.

  1. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    Hi All,

    For a few days now I have been struggling with a particular UNet problem.

    I have created a VR Game called VR Lawn Bowls (Crown Green) and I have successfully networked this game apart from the Vive Controllers.

    I have 2 versions,

    1) FPS which uses the Unity standard FPS controller and using Keys to select, and bowl the bowls, using keys to swap the Ownership of the
    NON Player Objects (4 x bowls per player, Max 4 players). This particular script is on the Player Prefab instantiated by the Network Manager and Hub.
    All Works very well.

    2) Exactly the same game but using Vive, VRTK along with the same scripts (All network aware including VRTK).
    All Non Player objects spawn correctly and are network aware, we can also bowl with the Vive controller using VRTK Grab options.
    Unfortunately the game mechanic starts to fail as the Vive Camera Rig and controllers are not Network aware, therefore I cannot change the
    owner of the objects, so as the physics start to interact they do not work. non owned bowls just stay in one position.

    i.e.

    I have a public function in the Main Network script (Displays ConnectionToClient) on the Player Prefab which I call from the TouchPad of the Vive Controller.
    This returns null, I test it running the function from a KeyBoard Key press from within the Script and it displays the fact we are ISLOCALPLAYER.

    public void FromViveController()
    {
    Debug.Log("Connection = " + connectionToClient);
    }


    I have done the following with no success.

    1) Network Identity on All Vive Controllers, Scripts, Bowls, even the UI Touch Panel that displays over the Vive Touch Pad.

    2) NetworkBehavioured every script including all the VRTK scripts.

    3) Even used the VIVE controllers and CamerRig as a Player Prefab so the Network Manager spawns it.

    I need it to know which player it is attached too, so when the game changes player that the controller can be monitored for picking up the wrong bowls.

    Am I missing something really stupid or even doing something that just isn't possible with UNet, Is started with Photon but found that UNet Physics seemed
    to run very well and smooth without too much extra coding.

    I know Photon have brought out TruSync which may do the task.

    Scripts and Screen Shots attached.



    Thanks

    Mike Hierarchy.PNG NetworkManager.PNG VR View.PNG
     

    Attached Files:

  2. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    If you're the local player, look at connectionToServer instead.
     
  3. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    I will try that now, but I am pretty sure that was null as well.
     
  4. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    Both Still Null

    Connection.PNG
     
  5. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    I admit I didn't read through your code, but I'll overview my understanding of this and maybe it will point you in the right direction.

    Any object you want to sync client motion on or send commands with, will need to be
    A) Instantiated on the server ONLY (handled by network manager for spawned player objects)
    B) Spawned on the network with NetworkServer.Spawn.(handled by network manager for spawned player objects)
    C) Given client authority by making it a player or adding client authority (handled by network manager for spawned player objects)

    Those are the only objects that need that treatment.

    Be aware that if you wind up trying to remove client authority from one of your objects
    A) You just can't if it's a player, so better to use AddClientAuthority and RemoveClientAuthority, and
    B) There is a bug in the interpolation code in NetworkTransform that causes objects to reset to an incorrect position when authority is removed (see PR#19 in the bitbucket repo for a fix.)
     
  6. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    Hi,

    Yes thank you, that is exactly what I am doing, and works perfectly in the FPS version where I am using keyboard commands to Remove Authority and Assign accordingly.

    It's just when I want to use the Controller to do various things as it is not ISLOCALPLAYER aware, if I use (islocalplayer) the controller functions do not fire.

    Mike
     
  7. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    You'd need to give authority to the controller objects, and they'd need to be spawned on the network. If that's not possible or desirable, call methods on objects that DO have authority from your controller inputs.

    IsLocalPlayer should only be true for objects spawned as a player, btw. Use hasAuthority if you are not setting player objects, and be aware there's another bug which sets authority on all objects with "LocalPlayerAuthority" to true when they are started on the server, so you can wind up with objects that claim to have authority both places if you don't take care of that. Look at PR#18 if you need a fix for that one.
     
    Last edited: Jun 12, 2017
  8. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    I must admit I am not Spawning the CameraRig or controllers as they are already in the scene.

    Do you think they need spawning as well.

    Will try it now.

    Mike
     
  9. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    As far as I can see all the methods I am firing from the Controller Do have authority as all these are attached to the Player Prefab.

    Mike
     
  10. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    I would highly recommend you not spawn the controllers because then the controllers will be duplicated to all your clients.
     
  11. angusmf

    angusmf

    Joined:
    Jan 19, 2015
    Posts:
    261
    If they are components on the same gameobject you spawned as a player, then anything that inherits from NetworkBehaviour should return true for IsLocalPlayer and hasAuthority, and I would expect connectionToServer to not be null.
     
  12. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    I totally agree, but it's almost like the VIVE controller / CameraRig has no awareness of it at all.

    The HorseMask below is what all my Network Scripts are attached to including the Main CreateNetworkBowls of which I am calling the below function from the VIVE.

    I have even put NetworkIdentities on the Vive CamerRig



    public void FromViveController()
    {
    Debug.Log("CreateNewBowls()");
    Debug.Log("Connection Client = " + connectionToClient);
    Debug.Log("Connection Server = " + connectionToServer);
    }


    vive Panel.PNG

    NetworkManager.PNG
    CameraRig.PNG
     
  13. robochase

    robochase

    Joined:
    Mar 1, 2014
    Posts:
    244
    Hey here's the approach I'm using - it seems to work well.

    1. Create a player object that is separate from your VR rig. It will have 3 game objects on it for your head & hands. If you want to do this as simply as possible, put a network transform component on the root object and a network transform child component on each of your game objects for the body parts.
    2. Set up your network manager to use the player object for players.
    3. Add a new script to your player object. Give this script an OnStartLocalPlayer function - use it to find your VR rig. Then in this script's update function, if isLocalPlayer is true, it will just match the world positions of your body part game objects to the world positions of the vive remotes/headset.
    4. That's it! The network transform should start sending the positions to other clients automatically.
     
    ertopena likes this.
  14. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    Thank you, I will give that a try.

    Mike
     
  15. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    Hi,

    Ok I have done that but I still have the same issues.

    I cannot fire anything from the Controllers touch pad, or if I do it isn't network aware.

    I can pick up the bowls etc and throw them, but I am unable to communicate anything to the game via the Controllers.

    If I try and fire a [Command] to update SyncVars or the likes I get the following error and still get no connection.

    Command function Cmd_SetGameInProgress called on server.
    UnityEngine.Debug:LogError(Object)

    Trying to send command for object without authority.
    UnityEngine.Networking.NetworkBehaviour:SendCommandInternal(NetworkWriter, Int32, String)

    Connection Server =
    UnityEngine.Debug:Log(Object)

    Connection Client =
    UnityEngine.Debug:Log(Object)

    I get the following when running the same commands from Keyboard entries within CreateNewBowls();

    Connection Client = hostId: -1 connectionId: 0 isReady: True channel count: 0
    UnityEngine.Debug:Log(Object)

    Connection Server = hostId: -1 connectionId: 0 isReady: True channel count: 0
    UnityEngine.Debug:Log(Object)

    And works absolutely fine.

    Mike
     
  16. knighthawk

    knighthawk

    Joined:
    Nov 30, 2014
    Posts:
    34
    All sorted,

    I have had to Delegate the relevant functions and subscribe from the scripts in the Player Prefab.

    I now get the correct connection status when pressing from the Controller.

    Mike
     
    GhostCoders likes this.