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

145 views Asked by At

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.

 [Command]
    public void CmdSpawnCapsule()
    {

        RpcSpawnCapsule();
    }

    [ClientRpc]
    public void RpcSpawnCapsule()
    {
        if (isLocalPlayer)
        {
            //Debug.Log("You are in the cmd");
            Vector3 capDir = transform.forward;
            Vector3 capPos = transform.position + capDir * 1.5f;

            GameObject capsuleToSpawn = (GameObject)Instantiate(capsule, sharedWorldAnchorTransform.InverseTransformPoint(capPos), Quaternion.Euler(capDir));
            //capsuleToSpawn.GetComponentInChildren<Rigidbody>().velocity = capDir;
            NetworkServer.SpawnWithClientAuthority(capsuleToSpawn, connectionToClient);
            //Debug.Log("Exiting cmd");
        }
    }

    public void OnInputClicked(InputClickedEventData eventData)
    {
        if (isLocalPlayer)
        {
            if (Physics.Raycast(transform.position, direction: transform.forward, hitInfo: out hit, maxDistance: range))
            {
                objectID = GameObject.Find(hit.transform.name);
                CmdAssignAuthority(objectID);
            }
        }
    }

    public void OnSpeechKeywordRecognized(SpeechKeywordRecognizedEventData eventData)
    {
        if (isLocalPlayer)
        {
            String keyword = eventData.RecognizedText;
            //Debug.Log(keyword);
            switch (keyword)
            {
                case "Spawn":

                    CmdSpawnCapsule();
                    break;


            }
        }
    }

    [Command]
    void CmdAssignAuthority(GameObject obj)
    {
        if (control == 0)
        {
            objNetId = obj.GetComponent<NetworkIdentity>();
            objNetId.AssignClientAuthority(connectionToClient);
            control = 1;
        }
        else
        {
            objNetId.RemoveClientAuthority(connectionToClient);
            control = 0;
        }

    }
1

There are 1 answers

0
Dtb49 On BEST ANSWER

Solved: The problem was in my GameObject.Find function where I was looking for an object name. I gave a unique name to each object and it solved the problem I was having.