Search Unity

UNET- non-player object glitches back and forth after moving from previous spot to current

Discussion in 'UNet' started by dbarrett, Sep 15, 2017.

  1. dbarrett

    dbarrett

    Joined:
    Sep 6, 2017
    Posts:
    32
    I am using Unet in Unity and I am trying to basically pick up an object and move it around the scene and show it to everyone on the server. I can successfully spawn the object with the host and both the client and host can see the object.

    However, as the client I can spawn the object but, it does not show up for the host. As far as moving the object the host is able to move the object and the client sees that the host is moving the object. I do this using Assign and Remove Authority method. However, when the client tries to move the host's object it does not appear on the server either. But when the host tries to move the object again after the client has "moved" it the object basically jumps repeatedly back and forth between where the host is moving it too and where the client placed it last.

    I really hoped that made sense and someone can help me out please. I have listed some code from the player controller script.
    Code (CSharp):
    1. [Command]
    2.     public void CmdSpawnCapsule()
    3.     {
    4.  
    5.         RpcSpawnCapsule();
    6.     }
    7.  
    8.     [ClientRpc]
    9.     public void RpcSpawnCapsule()
    10.     {
    11.         if (isLocalPlayer)
    12.         {
    13.             //Debug.Log("You are in the cmd");
    14.             Vector3 capDir = transform.forward;
    15.             Vector3 capPos = transform.position + capDir * 1.5f;
    16.  
    17.             GameObject capsuleToSpawn = (GameObject)Instantiate(capsule, sharedWorldAnchorTransform.InverseTransformPoint(capPos), Quaternion.Euler(capDir));
    18.             //capsuleToSpawn.GetComponentInChildren<Rigidbody>().velocity = capDir;
    19.             NetworkServer.SpawnWithClientAuthority(capsuleToSpawn, connectionToClient);
    20.             //Debug.Log("Exiting cmd");
    21.         }
    22.     }
    23.  
    24.     public void OnInputClicked(InputClickedEventData eventData)
    25.     {
    26.         if (isLocalPlayer)
    27.         {
    28.             if (Physics.Raycast(transform.position, direction: transform.forward, hitInfo: out hit, maxDistance: range))
    29.             {
    30.                 objectID = GameObject.Find(hit.transform.name);
    31.                 CmdAssignAuthority(objectID);
    32.             }
    33.         }
    34.     }
    35.  
    36.     public void OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData)
    37.     {
    38.         if (isLocalPlayer)
    39.         {
    40.             String keyword = eventData.RecognizedText;
    41.             //Debug.Log(keyword);
    42.             switch (keyword)
    43.             {
    44.                 case "Spawn":
    45.  
    46.                     CmdSpawnCapsule();
    47.                     break;
    48.  
    49.  
    50.             }
    51.         }
    52.     }
    53.  
    54.     [Command]
    55.     void CmdAssignAuthority(GameObject obj)
    56.     {
    57.         if (control == 0)
    58.         {
    59.             objNetId = obj.GetComponent<NetworkIdentity>();
    60.             objNetId.AssignClientAuthority(connectionToClient);
    61.             control = 1;
    62.         }
    63.         else
    64.         {
    65.             objNetId.RemoveClientAuthority(connectionToClient);
    66.             control = 0;
    67.         }
    68.  
    69.     }
     
    Last edited: Sep 18, 2017
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Use code tags, your code is hard to read otherwise.

    You can't spawn network objects on clients.

    You said you're running into your problem when the host moves the object. Where in this code is that?
     
  3. Xype

    Xype

    Joined:
    Apr 10, 2017
    Posts:
    339
    Joe, you can with command. Basically he is telling the server to spawn it, then the server is spawning it with client authority.

    Question, are you spawning a prefab that has a network identity script attached? Everything needs a network identity or to be attached to something with a network identity or the server wont keep up with it.

    Even shoot an arrow, the arrow needs a network identity, so silly.
     
  4. dbarrett

    dbarrett

    Joined:
    Sep 6, 2017
    Posts:
    32
    Sorry I thought I added the code brackets. But yes there is a network identity attached to the prefab that is being spawned as well as a network transform.

    Basically when the user uses the keyword spawn a capsule appears. When the user clicks on the capsule the user is granted authority over that object and is able to move the capsule around. Once the user clicks again authority is removed and the user drops the capsule. The move mechanics are part of another script but, they work fine. This script is attached to each player. It has a few more lines associated with it but nothing else that deals with spawning capsules.

    Like I said everything works fine on the host device and the client can see when the host moves the capsule and spawns a capsule. But the client cannot do anything to manipulate the scene.

    i.e. Cannot spawn or move a capsule over the network.

    EDIT:
    I can successfully spawn to the server from the client but the client still cannot move the object

    I threw a couple debug statements into the Assign Authority method and it never executes for the client. Any suggestions?
     
    Last edited: Sep 18, 2017
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I was responding to "However, as the client I can spawn the object but, it does not show up for the host."
     
  6. dbarrett

    dbarrett

    Joined:
    Sep 6, 2017
    Posts:
    32
    I found the error I had as for why the client could not spawn. The If(isLocalPlayer) needed to be outside the command. However, I believe there is something wrong with how I am calling the CmdAssignAuthority. As I stated above I put in some debug statements and I never get them in the log.

    I also ran the code with break points and it reaches the Command but, the client never executes it. @Xype

    I have also noticed that the host can "see" the object as it moves but it only does not render it on the host side. I only noticed this because as the client when I pass the host while the client is holding the object the host's cursor changes as if there is an object in front of it because of a script it knows when it is looking at an object.
     
    Last edited: Sep 18, 2017