Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

door in network

Discussion in 'Multiplayer' started by RensDevolp, Aug 10, 2014.

  1. RensDevolp

    RensDevolp

    Joined:
    Aug 10, 2014
    Posts:
    31
    hello everybody,
    i am making a multiplayer fps whit doors, but the doors only works with one person. im yousing pun networking. i hate tutorials. can someone please give me some scripts or tips?

    thanks a lot,

    Rens:)
     
  2. JamesElvin

    JamesElvin

    Joined:
    Sep 19, 2013
    Posts:
    33
    Use an RPC to open the door.

    Code (CSharp):
    1. //Set pv so it stands for the photon-view component
    2. PhotonView pv = PhotonView.Get(this);
    3.  
    4. //Call the RPC whenever you want to open the door
    5. pv.RPC("openDoor, PhotonTargets.All null);  
    6.  
    7. [RPC]
    8. void openDoor()
    9. {
    10. //Open the door here
    11. }
     
  3. RensDevolp

    RensDevolp

    Joined:
    Aug 10, 2014
    Posts:
    31
    Thanks a lot!
     
  4. RensDevolp

    RensDevolp

    Joined:
    Aug 10, 2014
    Posts:
    31
    mm, tryed it out but he still doesnt work, he keeps giving the error unexpected symbol ( and ). this is my script at the moment:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class open_door : MonoBehaviour
    7. {
    8.     PhotonView pv = PhotonView.Get(this);
    9.     public float smooth = (float)2.0;
    10.     public float DoorOpenAngle = (float)110.0;
    11.     public float DoorCloseAngle = (float)0.0;
    12.     public bool open = false;
    13.     public bool enter = false;
    14.     public string defined_key = "tab";
    15.     rv.RPC ("OnTriggerEnter", "OnTriggerExit", PhotonTargets.All);
    16.     // Update is called once per frame
    17.     void Update ()
    18.     {
    19.         if(open == true)
    20.         {
    21.             var target = Quaternion.Euler (transform.localRotation.x, DoorOpenAngle, transform.localRotation.z);
    22.             // Dampen towards the target rotation
    23.             transform.localRotation = Quaternion.Slerp(transform.localRotation, target,
    24.             Time.deltaTime * smooth);
    25.         }
    26.    
    27.         if(open == false)
    28.         {
    29.             var target1 = Quaternion.Euler (transform.localRotation.x, DoorCloseAngle, transform.localRotation.z);
    30.             // Dampen towards the target rotation
    31.             transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
    32.             Time.deltaTime * smooth);
    33.         }
    34.    
    35.         if(enter == true)
    36.         {
    37.             if(Input.GetKeyDown(defined_key))
    38.             {
    39.                 open = !open;
    40.             }
    41.         }  
    42.     }
    43.  
    44.     //Activate the Main function when player is near the door
    45. [RPC]  
    46. void OnTriggerEnter(Collider other)
    47.     {
    48.         if (other.gameObject.tag == "Player")
    49.         {
    50.             //Debug.Log("Trigger Enter");
    51.             (enter) = true;
    52.         }
    53.     }
    54.    
    55.     //Deactivate the Main function when player is go away from door
    56.     [RPC]
    57. void OnTriggerExit (Collider other)
    58.     {
    59.         if (other.gameObject.tag == "Player")
    60.         {
    61.             //Debug.Log("Trigger Exit");
    62.             (enter) = false;
    63.         }
    64.     }
    65.  
    66. }
    67.  
     
  5. JamesElvin

    JamesElvin

    Joined:
    Sep 19, 2013
    Posts:
    33
    You put 'rv' before the RPC instead of 'pv' which you defined as the photon view. Also you need to call the RPC when you want the door to open.
     
  6. RensDevolp

    RensDevolp

    Joined:
    Aug 10, 2014
    Posts:
    31
    still keeps giving the error :Assets/DesertFortressPack/Scripts/open_door.cs(15,16): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration and Assets/DesertFortressPack/Scripts/open_door.cs(15,16):
    and this error:
    Assets/DesertFortressPack/Scripts/open_door.cs(15,69): error CS1519: Unexpected symbol `)' in class, struct, or interface member declaration
     
  7. JamesElvin

    JamesElvin

    Joined:
    Sep 19, 2013
    Posts:
    33
    1. Alright, do this. In the trigger enter and exit call the RPC.
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.tag == "Player")
    4.         {
    5.             pv.RPC("openDoor", PhotonTargets.All, null);
    6.         }
    7.     }
    8.  
    9. void OnTriggerExit(Collider other)
    10.     {
    11.         if (other.gameObject.tag == "Player")
    12.         {
    13.             pv.RPC("closeDoor", PhotonTargets.All, null);
    14.         }
    15.     }
    16.  
    17. [RPC]
    18. void closeDoor()
    19. {
    20. //Debug.Log("Trigger Exit");
    21. (enter) = false;
    22. }
    23.  
    24. [RPC]
    25. void openDoor()
    26. {
    27. //Debug.Log("Trigger Enter");
    28. (enter) = true;
    29. }
     
  8. RensDevolp

    RensDevolp

    Joined:
    Aug 10, 2014
    Posts:
    31
    ok, think that worked, but now he gives me he message: Assets/DesertFortressPack/Scripts/open_door.cs(10,40): error CS0027: Keyword `this' is not available in the current context. What dit i do wrong?

    rens


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5.  
    6. public class open_door : Photon.MonoBehaviour
    7. {
    8.     PhotonView pv = PhotonView.Get(this);
    9.     public float smooth = (float)2.0;
    10.     public float DoorOpenAngle = (float)110.0;
    11.     public float DoorCloseAngle = (float)0.0;
    12.     public bool open = false;
    13.     public bool enter = false;
    14.     public string defined_key = "tab";
    15.  
    16. void OnTriggerEnter(Collider other)
    17. {
    18.     if (other.gameObject.tag == "Player")
    19.     {
    20.             Debug.Log("triggerenter");  
    21.             pv.RPC("openDoor", PhotonTargets.All, null);
    22.             (enter) = true;
    23.     }
    24. }
    25.  
    26. void OnTriggerExit(Collider other)
    27. {
    28.     if (other.gameObject.tag == "Player")
    29.     {
    30.             Debug.Log("triggerexit");  
    31.             pv.RPC("closeDoor", PhotonTargets.All, null);
    32.             (enter) = false;
    33.     }
    34. }
    35.  
    36. [RPC]
    37. void closeDoor()
    38. {
    39.         if(enter == true)
    40.         {
    41.             if(Input.GetKeyDown(defined_key))
    42.             {
    43.                 open = !open;
    44.             }
    45.         }  
    46.         if(open == true)
    47.         {
    48.             var target = Quaternion.Euler (transform.localRotation.x, DoorOpenAngle, transform.localRotation.z);
    49.             // Dampen towards the target rotation
    50.             transform.localRotation = Quaternion.Slerp(transform.localRotation, target, Time.deltaTime * smooth);
    51.         }
    52.  
    53. }
    54.  
    55. [RPC]
    56. void openDoor()
    57. {
    58.         if(open == false)
    59.         {
    60.             var target1 = Quaternion.Euler (transform.localRotation.x, DoorCloseAngle, transform.localRotation.z);
    61.             // Dampen towards the target rotation
    62.             transform.localRotation = Quaternion.Slerp(transform.localRotation, target1,
    63.                                                        Time.deltaTime * smooth);
    64.         }
    65.  
    66. }
    67. }
    68.