Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Third Party Photon Cloud Health losing Problem

Discussion in 'Multiplayer' started by TTF, Jan 26, 2014.

  1. TTF

    TTF

    Joined:
    Dec 1, 2013
    Posts:
    19
    Dear Unity programmers,

    First of all I want to say that I would be very grateful of any answer even if its just a little issue.
    If something about the logic of my game or the scripts isn't clear, just write me and I'm going to try to explain you with my bad english more precisely what's meant!
    I have a problem with networked health losing. If I shoot a cube with a PhotonView and so on, all works fine but if another player is joinig the problems start. When my player shoots the second player, the second player loses health correctly but oddly my player loses health too what I definitely don't want! I ve spent so many hours to find the reason for. But whatever I did, it failed. I would appreciate very much any kind of help.

    I must also mention that if I do the RPC after a raycast hit all works fine, but I am going to make an archer game where the collision detection script of the arrow causes the RPC. I called this arrow script 'UpdateRotation' because it ensures that the arrow turns towards the correct direction (bottom).
    Here is it:

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4.  
    5. public class UpdateRotation : MonoBehaviour {
    6.     bool Colliding = false;
    7.     GameObject coll;
    8.     public int damage = 50;
    9.     int collphotonviewid;
    10.  
    11.     void Update () {
    12.         if (!Colliding)//while the arrow is in the air and not colliding adjust the rotation
    13.             transform.forward = Vector3.Slerp(transform.forward, rigidbody.velocity.normalized, 10 * Time.deltaTime);
    14.         else{
    15.                         //if the arrow collides make it stuck in the ground or other Player
    16.             rigidbody.constraints = RigidbodyConstraints.FreezeAll;
    17.             if (coll.GetComponent<Health>() != null )//Check if the collided object has a health script
    18.                          {
    19.                 Colliding = false;
    20.                 collphotonviewid = coll.GetComponent<PhotonView>().viewID;
    21.                                 //I added this collphotonviewid parameter  in one of my attempts to make sure that everyone receives the right thing
    22.                 Debug.Log("Rot : collviewid = " + collphotonviewid);
    23.                 coll.GetComponent<PhotonView>().RPC("TakeDamage", PhotonTargets.All, damage, collphotonviewid);
    24.                 PhotonNetwork.Destroy( gameObject );
    25.             }
    26.             GetComponent<CapsuleCollider>().enabled = true;//enable picking up
    27.         }
    28.     }
    29.  
    30.     void OnCollisionEnter(Collision col){//check collision
    31.         if (col.gameObject.name != "Arrow(Clone)"  !Colliding){
    32.             Colliding = true;
    33.             coll = col.gameObject;
    34.         }
    35.     }
    36.  
    37. //This is that you can pick up the arrow from the ground
    38.     void OnTriggerEnter(Collider col){
    39.         if (col.gameObject.tag == "Player"  Colliding == true) {
    40.             col.gameObject.GetComponent<PlayerAttacking> ().Arrows += 1;
    41.             PhotonNetwork.Destroy( gameObject );
    42.         }
    43.     }
    44. }
    45.  
    46.  
    The arrow dropping script(just that you know how I handle this, I think the error isn't here):

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4.  
    5. public class PlayerAttacking : MonoBehaviour {
    6.  
    7.  
    8.     public float ArrowSpeed = 120.0f;
    9.     public float fireRate = 1.5f;
    10.     public int Arrows = 10;
    11.     float StartTime = 0.0f;
    12.     float maxPull = 2f;
    13.  
    14.     void Update ()
    15.     {
    16.         if(Input.GetButtonDown("Fire1"))
    17.         {
    18.  
    19.             StartTime = Time.time;//start measuring the pullback time
    20.            
    21.         }
    22.  
    23.         if(Input.GetButtonUp("Fire1"))
    24.         {
    25.             if (Arrows > 0){
    26.  
    27.                 GameObject arrow = PhotonNetwork.Instantiate("Arrow", Camera.main.transform.position, Camera.main.transform.rotation, 0);
    28.  
    29.                 float timepulledback = Time.time - StartTime;
    30.                 if (timepulledback > maxPull)
    31.                     timepulledback = maxPull;
    32.  
    33.                 float arrowSpeed = ArrowSpeed * timepulledback;//the arrow speed to increase damage(set below)
    34.  
    35.                 Physics.IgnoreCollision(arrow.collider, transform.collider);//ensure that the arrow doesn't collides with us
    36.  
    37.                 Arrows -= 1;
    38.  
    39.                 arrow.rigidbody.AddForce(Camera.main.transform.forward * arrowSpeed);//AddForce...
    40.                 arrow.GetComponent<UpdateRotation>().damage += (int)arrowSpeed/2;//increase the UpdateRotation script's damage by the arrowSpeed
    41.                 //Debug.Log("ArrowID = " + arrow.GetComponent<PhotonView>().viewID);
    42.             }
    43.             else
    44.                 Arrows = 0;
    45.         }
    46.    
    47.     }
    48.  
    49. }
    50.  
    51.  
    And now the script responsible for Health(I think the error isn't here):

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4.  
    5. public class Health : Photon.MonoBehaviour {
    6.  
    7.     public int CurrentHealth = 500;
    8.     int photonviewid;
    9.  
    10.     void Start(){
    11.         photonviewid = photonView.viewID;
    12.         Debug.Log ("Start:MyID = " + photonviewid);
    13.     }
    14.  
    15.     [RPC]
    16.     public void TakeDamage(int damge, int target){
    17.         Debug.Log (this.photonviewid + "---" + target);
    18.  
    19.         if (target == this.photonviewid)
    20.             CurrentHealth -= damge;
    21.         else
    22.             Debug.Log("not my ID!!!");//this error never appears
    23.  
    24.         if (CurrentHealth <= 0) {
    25.             Die();
    26.         }
    27.     }
    28.  
    29.     void Die(){
    30.         if (GetComponent<PhotonView> ().instantiationId == 0) {
    31.             Destroy (gameObject);
    32.         }
    33.         else {
    34.             if (GetComponent<PhotonView> ().isMine)
    35.                     PhotonNetwork.Destroy(gameObject);
    36.         }
    37.     }
    38. }
    39.  
    40.  
    41.  
    By Debugging (Debug Message:::Debug.Log (this.photonviewid + "---" + target); :::comes up) I found put that at some points the player hits himself. In my opinion it's not caused by the UpdateRotation script (Debug Message:::Debug.Log("Rot : collviewid = " + collphotonviewid; :::doesn't come up). But it must have to do with the UpdateRotation script, because this is the only RPC I have.


    Maybe I have set up the scene completely wrong. Just write if I should send you some screenshots of the inspector.

    I would be most grateful if you would look into this matter as soon as possible

    Tim
     
    Last edited: Jan 26, 2014
  2. TTF

    TTF

    Joined:
    Dec 1, 2013
    Posts:
    19
    It would also be great if anyone knows a tutorial or demo of a multiplayer game where the projectile has a collider wich causes the health losing...
     
  3. TTF

    TTF

    Joined:
    Dec 1, 2013
    Posts:
    19
    Come on please help me. The only oddly thing I could mention is that when I make the arrow a child of the collided object the arrow appears to be always stucking in the other player. Example:

    Player A shoots Player B. Player A sees the arrow stucked in Player B, whats right. But when you are Player B and watch Player A you see the arrow that Player A has fired stucking in Player A.

    Maybe this helps you to find the problem. I have really no idea.
     
    Last edited: Jan 30, 2014
  4. Sickuhtrix

    Sickuhtrix

    Joined:
    Nov 20, 2013
    Posts:
    62
    Upload your project some where and PM me the link, I can't take a look right this minute, but this is probably a simple fix.
     
  5. TTF

    TTF

    Joined:
    Dec 1, 2013
    Posts:
    19
    Ok I've waited now three days for a response from Sickuhtrix. Maybe it hasn't been sent correctly.
    Well - I'm gonna qive the link to project files now here. It would be very, very friendly when someone could take a minute for this and help me fixing it.
    I do not know how I could express my gratitude in English, if someone could find the reason for the problem.

    Attachement Link:

    https://www.dropbox.com/s/2nbw7rykpzqskl6/Mission%20Survive%20Project%20-%20Thanks.zip
     
    Last edited: Jan 30, 2014
  6. TTF

    TTF

    Joined:
    Dec 1, 2013
    Posts:
    19
    *push*
     
  7. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    3,018
    I can't debug your project but ...

    This sounds like the arrow was able to hit player A on player B's machine. You might want to find out how you can ignore collisions of Player A and his arrows or you want to spawn the arrow far enough in front of player A to not hit it.
     
  8. TTF

    TTF

    Joined:
    Dec 1, 2013
    Posts:
    19
    Thanks a lot and sorry for the late answer.
    I tried to ignore the collision by:

    Code (csharp):
    1.  
    2. Physics.IgnoreCollision(Player, Arrow);
    3.  
    Isn't that correct?
     
  9. KuPAfoo

    KuPAfoo

    Joined:
    Aug 24, 2013
    Posts:
    17
    Should include a private photonview myphotonview;


    and in the
    Code (csharp):
    1. start(){myPhotonView = PhotonView.Get(this);}
    Code (csharp):
    1.  
    2. void Update () {
    3.  
    4.         if (!Colliding)//while the arrow is in the air and not colliding adjust the rotation
    5.  
    6.             transform.forward = Vector3.Slerp(transform.forward, rigidbody.velocity.normalized, 10 * Time.deltaTime);
    7.  
    8.         else {
    9.  
    10.                         //if the arrow collides make it stuck in the ground or other Player
    11.  
    12.             rigidbody.constraints = RigidbodyConstraints.FreezeAll;
    13.  
    14.             if (coll.GetComponent<Health>() != null )//Check if the collided object has a health script
    15.  
    16.                          {
    17.  
    18.                 Colliding = false;
    19.  
    20.                 collphotonviewid = coll.GetComponent<PhotonView>().viewID;
    21.  
    22.                                 //I added this collphotonviewid parameter  in one of my attempts to make sure that everyone receives the right thing
    23.  
    24.                 Debug.Log("Rot : collviewid = " + collphotonviewid);
    25.  
    Code (csharp):
    1.  
    2.                 PhotonNetwork.Destroy( gameObject );
    3.  
    4.             }
    5.  
    6.             GetComponent<CapsuleCollider>().enabled = true;//enable picking up
    7.  
    8.         }
    9.  
    10.     }
    11.  
     
  10. TTF

    TTF

    Joined:
    Dec 1, 2013
    Posts:
    19
    Boah thank you very very much for your help. All works fine. I hope you were able to understand most of my sentences. I'm still learning english:D

    It works pretty well if I start it from the compiled standalone executables. I know it sounds maybe unbelievable but if one Client get started in the Unity Editor then Unity just crashes with no error messages. Does somebody once had a similar issue and has known how to fix it? How can I turn off the debugger to see if it works without a debugger?

    Thanks for your awesome effort
     
    Last edited: Feb 22, 2014
  11. TTF

    TTF

    Joined:
    Dec 1, 2013
    Posts:
    19
    *push*