Search Unity

Third Party Mirror Trying to throw items.

Discussion in 'Multiplayer' started by Jackalop, Sep 5, 2021.

  1. Jackalop

    Jackalop

    Joined:
    Oct 20, 2018
    Posts:
    6
    I'm trying to where I can pick up and throw items using mirror. Currently I am doing this by deleting the object on the ground and spawning a different non networked prefab into the players hand. To throw the item, The hand item is deleted and a new object is spawned and launched by the server. It works fine on the host, but it breaks on the client. One of the issues is that there is noticeable lag between the object being spawned and it being launched. The other issue is that the object doesn't seem to always spawn in the right place or launch correctly. The item being thrown is on a separate layer from my player collider, and I tried disabling all of the hitbox colliders on the player, but I still can't get it to work.
    Here's my code:
    Code (CSharp):
    1. [Command]
    2.     private void CmdThrow(int i, float itemMass)
    3.     {
    4.         float throwForce = 7.28f * Mathf.Pow(10f, -0.0623f * itemMass);
    5.         if (throwForce > 7) { throwForce = 7f; }
    6.         if (throwForce < 3) { throwForce = 3f; }
    7.         if (itemMass > 99) { throwForce = 7f; }
    8.         GameObject playerWithItem = NetworkIdentity.spawned[netId].gameObject;
    9.         Transform newItemContainer = playerWithItem.transform.GetChild(0).GetChild(0).GetChild(2).GetChild(0).GetChild(0).GetChild(2).GetChild(0).GetChild(0).GetChild(1);
    10.         Transform _cam = playerWithItem.transform.GetChild(0).GetChild(0).GetChild(2).GetComponentInChildren<Camera>().transform;
    11.         Transform arm = playerWithItem.transform.GetChild(0).GetChild(0).GetChild(2).GetChild(0).GetChild(0).GetChild(2).GetChild(0).GetChild(0);
    12.         //arm.gameObject.GetComponent<Collider>().enabled = false;
    13.         Debug.Log(grounditems[i].name);
    14.         //GameObject itemThrown = Instantiate(grounditems[i], playerWithItem.transform.position + _cam.forward, Quaternion.Euler(0,0,0));
    15.         GameObject itemThrown = Instantiate(grounditems[i], newItemContainer.position, Quaternion.Euler(0,0,0));
    16.         Rigidbody itemRB = itemThrown.GetComponent<Rigidbody>();
    17.         NetworkServer.Spawn(itemThrown);
    18.        
    19.         foreach (Collider col in itemThrown.GetComponents<Collider>())
    20.         {
    21.             foreach (Collider hitBox in playerWithItem.GetComponentsInChildren<Collider>())
    22.             {
    23.                 Physics.IgnoreCollision(hitBox, col, true);
    24.             }
    25.         }
    26.         itemRB.velocity = playerWithItem.GetComponent<Rigidbody>().velocity;
    27.         itemThrown.GetComponent<Rigidbody>().AddForce(_cam.forward * throwForce + new Vector3(0f, throwForce / 2, 0f), ForceMode.VelocityChange);
    28.         float random = Random.Range(10, 30);
    29.         itemThrown.GetComponent<Rigidbody>().AddTorque(new Vector3(random, random, random));
    30.         //StartCoroutine(Reconsider(itemThrown));
    31.     }
     
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Idk how to solve that, but instead of destroying the object in ground and instantiate one in the hand, you should have a object in the hand and, when pick up an item you activate it, if thrown, you deactivate it. With the grounded item something similar, deactivate it when picked up and activate it when thrown. Is called "Object Pooling", and will increase preformance, since instantiate/destroy objects is bad for preformance. Hope ith helps with the lag part :p
     
  3. Jackalop

    Jackalop

    Joined:
    Oct 20, 2018
    Posts:
    6
    Thanks for the reply. I'll definitely have to look into that. I originally tried to avoid activating and deactivating objects because I thought it would be annoying to do with a lot of objects but doing it the way I was trying ended up being more difficult than I thought.