Example #1
0
    IEnumerator WaitAndUnspawn(GameObject obj)
    {
        yield return(new WaitForSeconds(3f));

        ThrownStoneObjectPool.unspawnStone(obj); //on server
        NetworkServer.UnSpawn(obj);              //on clients
    }
Example #2
0
    public void CmdThrowStone()
    {
        if (playerInst.numStonesPossessed > 0)  //sort of for anti-cheating on client
        {
            GameObject stone = ThrownStoneObjectPool.GetFromPool(start.position);

            if (stone == null)  //no objects in pull, shouldn't happen
            {
                Debug.LogError("no objects in pool, increase pool size");
                return;
            }

            playerInst.numStonesPossessed -= 1;//syncVar changed, and hook called on server and clients


            Rigidbody2D rb = stone.GetComponent <Rigidbody2D>();
            rb.velocity = (end.position - start.position).normalized * stoneSpeed * Time.fixedDeltaTime / (rb.mass);//same as rb.AddForce(v* stoneSpeed, ForceMode2D.Force);

            NetworkServer.Spawn(stone);

            StartCoroutine(WaitAndUnspawn(stone));//1) if didn't collide with anything 2) if collided and was set invisible
        }
    }