Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Network Raycast

Discussion in 'Multiplayer' started by Sanity11, Jan 6, 2009.

  1. Sanity11

    Sanity11

    Joined:
    Nov 25, 2007
    Posts:
    96
    Hi everybody.
    I am trying to get some code to work over the network.
    The idea is that this code should work regardless of who does it. But it only works on the server. When I chance the RPCMode to Server it only works on the client,so that didn't work also.

    Code (csharp):
    1.  
    2. private var hitObjectPosition : Vector3;
    3. private var hitObjectRotation : Quaternion;
    4.  
    5. //Set communication with gameplay code to know how many players are available.
    6. private var gamePlay;
    7. gamePlay = GetComponent(NetworkGamePlay);
    8.  
    9. private var ray;
    10. private var hits : RaycastHit[];
    11. private var objectRenderer;
    12.  
    13. function Update ()
    14. {
    15.     if (Input.GetMouseButtonDown(0))
    16.     {
    17.         //Transform the mouse position on screen to a position in 3d enviroment.
    18.         ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    19.         networkView.RPC("ChangeColor", RPCMode.All);
    20.     }
    21. }
    22.  
    23. @RPC
    24. function ChangeColor ()
    25. {  
    26.     hits = Physics.RaycastAll (ray, 100.0);
    27.    
    28.     // Change the material of all hit colliders
    29.     for (var i=0;i<hits.length;i++)
    30.     {
    31.         var hit : RaycastHit = hits[i];
    32.         objectRenderer = hit.collider.renderer;
    33.         if (objectRenderer)
    34.         {
    35.             //objectRenderer.material.shader = Shader.Find("Transparent/Diffuse");
    36.             objectRenderer.material.color = gamePlay.playerColors[gamePlay.playerTurn];
    37.             hitObjectPosition = hit.transform.position;
    38.             hitObjectRotation = hit.transform.rotation;
    39.             Destroy (hit.collider);
    40.         }
    41.     }
    42.    
    43.     if (hits.length == 0)
    44.     {
    45.         hitObjectPosition = Vector3(0.0,0.0,0.0);
    46.     }
    47. }
    48.  
     
  2. Sanity11

    Sanity11

    Joined:
    Nov 25, 2007
    Posts:
    96
    I have this code here:

    Code (csharp):
    1.  
    2. @RPC
    3. function GetPos ()
    4. {  
    5.     ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    6.     hits = Physics.RaycastAll (ray, 100.0);
    7.     // Change the material of all hit colliders
    8.     for (var i=0;i<hits.length;i++)
    9.     {
    10.         var hit : RaycastHit = hits[i];
    11.         objectRenderer = hit.transform.renderer;
    12.         if (objectRenderer)
    13.         {
    14.             //objectRenderer.material.shader = Shader.Find("Transparent/Diffuse");
    15.             //objectRenderer.material.color = gamePlay.playerColors[gamePlay.playerTurn];
    16.             hitObjectPosition = hit.transform.position;
    17.             hitObjectRotation = hit.transform.rotation;
    18.             Destroy (hit.collider);
    19.         }
    20.     }
    21.    
    22.     if (hits.length == 0)
    23.     {
    24.         hitObjectPosition = Vector3(0.0,0.0,0.0);
    25.     }
    26. }
    27.  
    28.  

    I call it like this:

    Code (csharp):
    1.  
    2. networkView.RPC("GetPos", RPCMode.All);
    3.  
    But it only works on the server side. Why is that? Any pointer would be very much appreciated!

    Edit:
    Code (csharp):
    1.  
    2. ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    3.         networkView.RPC("GetPos", RPCMode.All);
    4.  
    Now I am calling it like this and removed the extra line from the above script. Like this I was hoping the ray position would be invoked from the machine the click was made on. But it isn't. It still uses the mouse position from the server :(