Search Unity

Having Trouble Discerning Ownership of Scripts when using ScriptName.instance.

Discussion in 'Multiplayer' started by akauper, Jun 16, 2013.

  1. akauper

    akauper

    Joined:
    Jun 16, 2013
    Posts:
    7
    Hello and thank you for any help in advance.

    For the last few months I've been creating a 3rd person RPG arena-combat game (think WoW Arena).
    I've completed enough for two players to play against each other and now I'm trying to tackle networking. I'm fairly new to the subject but I've watched 4 or 5 tutorials and I think I have the basic concepts down.

    However, when using certain commands to talk between scripts I'm running into trouble.

    Below I will attempt to demonstrate my problem as thoroughly as possible. If anything isn't clear please let me know.

    I've initialized a server and connected to the server as a client.

    I've gotten basic movement to send from client to server and vice-versa using RPC calls. For example,

    Script Name: PlayerController
    Code (csharp):
    1.  
    2. public class PlayerController extends MonoBehaviour {
    3.  
    4. static var instance: PlayerController;
    5.  
    6. function Awake () {
    7.       instance = this;
    8.  
    9.       if(!networkView.isMine)
    10.       {
    11.             enabled=false;
    12.       }
    13. }
    14.  
    15. function Update()
    16. {
    17.     if(networkView.isMine)
    18.     {
    19.         characterController.Move(playerDirWorld * Time.deltaTime);
    20.         transform.Rotate(rotation);
    21.        
    22.         if(Vector3.Distance(transform.position, lastPosition)>=0.05)
    23.         {
    24.             lastPosition=transform.position;
    25.            
    26.             networkView.RPC("SetPosition", RPCMode.Others, transform.position);
    27.         }
    28.         networkView.RPC("SetRotation", RPCMode.Others, transform.rotation);
    29.     }
    30. }
    31.    
    32. @RPC function SetPosition(newPos : Vector3)
    33. {
    34.     transform.position=newPos;
    35. }
    36. @RPC function SetRotation(newRot : Quaternion)
    37. {
    38.     transform.rotation=newRot;
    39. }
    40. }
    41.  
    NOTE: There is a chunk of code between the static var instance and the update function which I have not included. This code sets certain variables such as "playerDirWorld" and "characterController" (Set to the Character Controller attached to my gameobject). I have not included this code as it isn't relevant to my problem.

    The above scripts works perfectly and I am able to see independent movement of characters on client and server side.

    My problem arises when the script executing movement is not attached to the Player Gameobject itself. For example. The below Script is attached to my Main Camera, and is referencing my player GameObject using PlayerController.instance

    Script Name: CameraFunction
    Code (csharp):
    1.  
    2. public class CameraFunction extends MonoBehaviour
    3. {
    4. public static var instance : CameraFunction;
    5. function Awake()
    6. {
    7.     if (!networkView.isMine)
    8.     {
    9.         enabled=false;
    10.         camera.enabled = false;
    11.     }
    12. }
    13.  
    14. function Update ()
    15. {
    16. if (Input.GetMouseButton(1))
    17.     {
    18.         if(networkView.isMine)
    19.         {
    20.             PlayerController.instance.transform.rotation = Quaternion.Euler(PlayerController.instance.transform.eulerAngles.x, Camera.mainCamera.transform.eulerAngles.y, PlayerController.instance.transform.eulerAngles.z);
    21.  
    22.             networkView.RPC("SetRotation", RPCMode.Others, PlayerController.instance.transform.rotation);
    23.         }
    24.     }
    25. }
    26.  
    27. @RPC function SetRotation(newRotCam : Quaternion)
    28. {
    29.     PlayerController.instance.transform.rotation = newRotCam;
    30. }
    31. }
    32.  
    When I try to rotate my character using the right mouse button (MouseButton(1)), instead of rotating my character, it rotates the other character in the scene.
    My thinking is that because I'm telling the script what to move using PlayerController.instance.transform.rotation, the game looks for the first instance of the script "PlayerController" it can find, which in the hierarchy is the script attached to the opposing character, and then executes the rotation on that game object.

    My question is, how do I specify the ownership of a script like PlayerController? When I use PlayerController.instance.transform.rotation how do I make the game know I am speaking about the game object that player owns.


    To inform further, I am having the game use Network.Instantiate to place the prefab of both my Player and the Main Camera.

    I think there may be some fundamental flaw in my understanding of networking here.

    Thank you for your time to reply,

    Adam


    EDIT: if, in the above code, instead of if(networkView.isMine) i use if(PlayerController.instance.networkView.isMine) there is no rotation of the character when i hold the right mouse button. (with just networkView.ismine it rotates the opposing games character)
     
    Last edited: Jun 16, 2013