Search Unity

Weird destroying behaviour

Discussion in 'Multiplayer' started by TehGM, Apr 22, 2014.

  1. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    NOTE - Solved it already. After resting (aka sleep), I realized that when 2 missiles hit almost at once, both manage to start respawn function within that split second. Once conditional statement before that and it's fixed. Would delete the thread, but I see no option for that. Well, guess others might have use of this in some way.

    Heya, it's me having problem again.

    So well, long story short, I am making a SPAZ-like game where players have their ships and kill eachother. For now I have working missiles, but they seem to have problem destroying others in certain cases.
    So far, I want to make player able to host a game and play at once, while others just connect. Okay, so players start playing. One player shoots missiles at the host, he blows up, respawns and all is good.
    However, problem begins when the host tries to kill a connected player. They (host atleast) respawn doubled. I've been googling for a while to find out what is going on, but no success... or I am just not really understanding...
    Edit: Just now I've noticed multiplied spawning might be happening if ship has more than one missile launcher and target gets killed with not all launched at him (and some somehow attempt to RPC damage to the ship even though it does not exist anymore). I was about to not post this thread cause I changed one thing and it seemed to not happen anymore, but then I got Unity frozen cause of like thousand ships spawned at once in one location. Note, this time it was host killing himself, no other players connected at all.
    Well, it works like this:

    1. Player shoots the missile (missile is handled server-side)
    Code (csharp):
    1.     public void Fire(Missile missile, NetworkViewID target)
    2.     {
    3.         if ((allowedMissiles  missile.type) == missile.type  (allowedDamageTypes  missile.damageType) == missile.damageType)
    4.         {
    5.             if (loaded)
    6.             {
    7.                 StartCoroutine(Reload());
    8.                 if (Network.peerType == NetworkPeerType.Client)
    9.                     networkView.RPC("Launch", RPCMode.Server, missile.Index, target);
    10.                 else Launch(missile.Index, target);
    11.             }
    12.             else Debug.LogError("Please wait till missiles reload!");
    13.         }
    14.         else Debug.LogError("This launcher can't use this type of missiles!");
    15.     }
    16.  
    17.     [RPC]
    18.     void Launch(int MissileID, NetworkViewID target, NetworkMessageInfo nfo)
    19.     {
    20.         Missile missile = (Missile)Network.Instantiate(GameObject.FindObjectOfType<Statics>().missileTypes3D[MissileID],
    21.             transform.position, transform.rotation, 0);
    22.         missile.target = target;
    23.         missile.owner = nfo.sender;
    24.         missile.OwnerName = Manager.FindPlayerWithNetworkPlayer(nfo.sender).Name;
    25.     }
    2. Missile hits the ship and creates explosion
    Code (csharp):
    1.     void OnTriggerEnter(Collider _coll)
    2.     {
    3.         if (Network.isServer  blowable)
    4.         {
    5.             Debug.Log("Missile hit an object");
    6.             if (_coll.tag == "Player" || _coll.tag == "Entity" || _coll.tag == "Enemy")
    7.             {
    8.                 Blow();
    9.             }
    10.         }
    11.     }
    12.  
    13.     void Blow()
    14.     {
    15.         Explosion exp = (Explosion)Network.Instantiate(GameObject.FindObjectOfType<Statics>().Explosion3D, transform.position,
    16.             Quaternion.identity, 0);
    3. Explosion pops a sphere collider for 1 second and tries to damage every ship in it
    Code (csharp):
    1.     IEnumerator Fade()
    2.     {
    3.         yield return new WaitForSeconds(1);
    4.         Collider.enabled = false;
    5.         yield return new WaitForSeconds(5);
    6.         Network.RemoveRPCs(networkView.viewID);
    7.         Network.Destroy(gameObject);
    8.     }
    9.  
    10.     void OnTriggerEnter(Collider _coll)
    11.     {
    12.         if (networkView.isMine  (_coll.tag == "Player" || _coll.tag == "Enemy"))
    13.         {
    14.             if (causer != null)
    15.             {
    16.                 if (!_coll.networkView.isMine)
    17.                     _coll.networkView.RPC("TakeDamage", _coll.networkView.owner, damage, Edamage, Pdamage, causerName, causer);
    18.                 else Camera.main.GetComponent<PlayerCamera>().myShip.TakeDamage(damage, Edamage, Pdamage, causerName, causer);
    19.             }
    20.             else
    21.             {
    22.                 if (!_coll.networkView.isMine)
    23.                     _coll.networkView.RPC("TakeDamageNPC", _coll.networkView.owner, damage, Edamage, Pdamage, causerName);
    24.                 else Camera.main.GetComponent<PlayerCamera>().myShip.TakeDamageNPC(damage, Edamage, Pdamage, causerName);
    25.             }
    26.         }
    27.     }
    4. Ship receives damage and if armor is 0 or lower, explodes
    Code (csharp):
    1.     [RPC]
    2.     public void TakeDamage(int Amount, int EAmount, int PAmount, string Name, NetworkPlayer giver)
    3.     {
    4.         if (networkView.isMine)
    5.         {
    6.             Manager.Notif((Amount + EAmount + PAmount) + " damage from " + Name);
    7.             if (Shield - EAmount > 0)
    8.                 Shield -= EAmount;
    9.             else Shield = 0;
    10.             Armor -= PAmount;
    11.             if (Shield - Amount > 0)
    12.                 Shield -= Amount;
    13.             else
    14.             {
    15.                 int AmountLeft = Amount - Shield;
    16.                 Shield = 0;
    17.                 Armor -= AmountLeft;
    18.             }
    19.             if (Armor <= 0)
    20.                 Kill(Name, giver);
    21.         }
    22.     }
    23.  
    24.     void Kill(string KillerName, NetworkPlayer giver)
    25.     {
    26.         if (networkView.isMine)
    27.         {
    28.             Manager.SendNotif(string.Format("{0} has killed {1}", KillerName, Statics.PlayerName));
    29.             Explode();
    30.             Manager.StartCoroutine(Manager.Respawner());
    31.             Network.RemoveRPCs(networkView.viewID);
    32.             Network.Destroy(gameObject);
    33.         }
    34.     }
    5. And then respawns
    Code (csharp):
    1.     public IEnumerator Respawner()
    2.     {
    3.         yield return new WaitForSeconds(RespawnTimer);
    4.         SpawnPlayer();
    5.     }
    6.  
    7.     void SpawnPlayer()
    8.     {
    9.         PlayerShip ship = (PlayerShip)Network.Instantiate(rookieShip, Vector3.zero, Quaternion.identity, 0);
    10.         ship.SendPlayer(Network.player);
    11.         GameObject.FindObjectOfType<PlayerCamera>().myShip = ship;
    12.     }
    And so, as I said seems to work good when client attacks the host, but when host attacks the client, it just acts all strange. :confused:
    Anyone help? Maybe I do something wrong, and maybe some more info would be needed, but either way, I will appreciate all explanations and questions. Hope I can avoid necessity to make a dedicated server.
     
    Last edited: Apr 23, 2014