Search Unity

Spawn GameObject - New Multiplayer

Discussion in 'Multiplayer' started by YoPawel, Nov 21, 2015.

  1. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    Hi,
    I create my project multiplayer and have small problem.

    My Object Spawn don't have good position and rotation only Vector3(0,0,0)... for client.
    For Server work good!
    My code very long... share only problem
    Code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. using UnityEngine.Networking;
    6.  
    7. public class strzalraycast : NetworkBehaviour {
    8.  
    9.   public Camera takamera; // camera child this object
    10.  
    11.   public GameObject dziurakulatekstura; //prefab in assets
    12.  
    13. void Update()
    14.     {
    15.              if (!isLocalPlayer)
    16.               return;
    17.  
    18.  
    19. if (Input.GetMouseButtonDown(0))
    20. {
    21.              Ray ray = takamera.ScreenPointToRay(Input.mousePosition);
    22.  
    23.                 RaycastHit hit;
    24.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    25.                 {
    26.  
    27.               Cmdklonujdziure(hit);
    28.                }
    29.  
    30. }
    31.      }
    32.  
    33.  
    34.  
    35.  
    36.  
    37. [Command(channel = 1)]
    38.  
    39.      void Cmdklonujdziure(RaycastHit hit)
    40.  
    41.     {
    42.  
    43.         GameObject obiekt = (GameObject)Instantiate(dziurakulatekstura, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
    44.  
    45.         NetworkServer.SpawnWithClientAuthority(obiekt, connectionToClient);
    46.        NetworkServer.Spawn(obiekt);
    47.  
    48.  
    49.  
    50.     }
    51.  
    52. }
    53.  
    Of corse I don't have error and add registered prefabs in network manager

    Object spawn and player - add network identity, true - local player authority, add network transform

    Sry my english not the best

    If I delete command - work but server don't see object - (error)
     
  2. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
  3. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    Try NetworkServer.SpawnWithClientAuthority() or NetworkServer.Spawn, but not both.
     
  4. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    thx for answer but it not repair, still does not work ;/
    Why only client have problem ?
     
  5. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    Screen all information problem
     

    Attached Files:

  6. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    I find problem but i don't know how repair...

    Raycast don't work because hit.point = Vector3.zero

    I use Debug.DrawLine() and client don't have Line - Why ?


    Code (csharp):
    1.  
    2. //In this way, turn off the camera
    3.  
    4.   if (!isLocalPlayer)
    5.   {
    6.  
    7.   transform.FindChild("MainCamera").GetComponent<Camera>().enabled = false;
    8.   transform.FindChild("MainCamera").GetComponent<AudioListener>().enabled = false;
    9.  
    10.  
    11.   }
    12.  
     
  7. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    I change camera - all delete camera in prefab and add one in scene. - Of corse don't work. It is game fps ...
     
  8. Sepica

    Sepica

    Joined:
    Feb 4, 2014
    Posts:
    6
  9. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    I add in prefab, but nothing change ;/.
    Why raycast don't work client ?
     
  10. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    look scene problem... raycast don't work client
     

    Attached Files:

  11. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    The way i do it is, before you use the spawn command for obiekt.. on obiekt add in class a syncvar that will vector3 location and rotation, and ad a hook to them so when the syncvar changes it calls a function, in the function set your location and or rotation to what the syncvars are, then before the spawn command get a refrance to the objects class that contains the syncvars you just made, and set the location using the hitray and rotation as you need.
     
  12. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    Ok try, but why drawline raycast in client don't work??
     
  13. LeopardX

    LeopardX

    Joined:
    May 31, 2015
    Posts:
    64
    Not sure.. but i do it like this..

    Code (CSharp):
    1.   if (Input.GetButtonDown("Fire1"))
    2.         {  
    3. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    4.                     RaycastHit hit;
    5.                     if (Physics.Raycast(ray, out hit, 50f))
    6.                     {
    7.  
    8.                    
    9.                         Cmdrezobjectonserver(myObject, hit.point);
    10.                
    11.                     }
    12. }
    13.  
    14.     [Command]
    15.  
    16.     void Cmdrezobjectonserver(GameObject Obj, Vector3 hitx) {
    17.         GameObject go = GameObject.Instantiate(Obj, hitx, Quaternion.identity) as GameObject;
    18.  
    19.         NetworkServer.Spawn(go);
    20.      
    21.     }
    Make sure you add the myObject to your NetworkManager in the editor under register spawnable objects.. and that you have draged the prefab onto your dziurakulatekstura in the editor for that object.
     
    Last edited: Dec 5, 2015
    YoPawel likes this.
  14. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    Work , nice job LeopardX .
    Thank you very much


    if it comes to

    Debug.DrawLine(ray.origin, hit.point);
    work only server ?
    Can you use in client ?
     
    Last edited: Dec 4, 2015
  15. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    I have last quest ;/

    Why function "Quaternion.FromToRotation()" don't work? - client


    Code (csharp):
    1.  
    2.  
    3. ...
    4.   Cmdspawner( hit.point , hit.normal);
    5.  
    6. .....
    7.  
    8. [Command]
    9.   void Cmdspawner( Vector3 hitp , Vector3 hitn) {
    10. GameObject aa = GameObject.Instantiate(myObject, hitp, Quaternion.FromToRotation(Vector3.up, hitn) ) as GameObject;
    11.  
    12. NetworkServer.Spawn(aa);
    13. .....
    14.  
    15.  
    16.  
    17.  
     
    Last edited: Dec 5, 2015
  16. YoPawel

    YoPawel

    Joined:
    May 6, 2014
    Posts:
    19
    I repair, please close