Search Unity

How can I get an instance of a gameObject from the Server to the client using Unity's Unet?

Discussion in 'UNet' started by dbarrett, Oct 19, 2017.

  1. dbarrett

    dbarrett

    Joined:
    Sep 6, 2017
    Posts:
    32
    I am definitely just a beginner when it comes to UNET and I could really use some help or advice. Just something. Basically my game is set up as a host client architecture where whenever a player joins they are in a space basically and they can spawn a plane and fly it around the space they are in. I have this down as well as a basic shoot functionality. What I am working on now is something along the lines of missiles where the player looks at the enemy plane and it takes a ray cast and gets the collider info, etc. The problem I am having is getting the information back to the client. In order to do the "heat-seeking" missile, I am basically calling a Lerp function, but since it is not in the Update function I need to do it in a Co-routine. The problem with this is that I need the missile game object information in order to perform the lerp function on it. I have tried putting the Co-routine inside of the Command function but, it does not work on the client side and only works for the host. Here is the code I have so far on it.

    My Update Function:

    Code (CSharp):
    1. private void Update()
    2. {
    3. if (controllerInput.GetButtonDown(ControllerButton.A) && planeSpawned )
    4.             {
    5.                 if (isLocalPlayer)
    6.                 {
    7.                     bool raycastHit = Physics.Raycast(transform.position, direction: transform.forward, hitInfo: out hit, maxDistance: range);
    8.                     if (raycastHit && hit.transform.gameObject.CompareTag("Plane"))
    9.                     {
    10.                         enemyShip = hit.transform.gameObject;
    11.  
    12.                         CmdFireMissle();
    13.                        
    14.                     }
    15.                 }
    16.             }
    17. }
    Here is the Command:

    Code (CSharp):
    1.  [Command]
    2.         public void CmdFireMissle()
    3.         {
    4.             Vector3 bulletDir = planeToSpawn.transform.forward;
    5.             Vector3 bulletPos = planeToSpawn.transform.position + (bulletDir * (0.01f + 3 * planeToSpawn.transform.localScale.x));
    6.             misslePos = bulletPos;
    7.             // The bullet needs to be transformed relative to the shared anchor.
    8.             missleToSpawn = Instantiate(missle, sharedWorldAnchorTransform.InverseTransformPoint(bulletPos), Quaternion.LookRotation(bulletDir));
    9.             missleToSpawn.transform.localScale = planeToSpawn.transform.localScale * 0.1f;
    10.            
    11.             NetworkServer.Spawn(missleToSpawn);
    12.             StartCoroutine(MoveTo(missleToSpawn, misslePos, enemyShip, 1));
    13.             RpcPlayBulletAudio(planeToSpawn);
    14.  
    15.             // Clean up the missile in 15 seconds.
    16.             Destroy(missleToSpawn, 15.0f);
    17.          
    18.         }
     
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    1. Make missile prefab with NetworkIdentity and NetworkTransform
    2. Add rigidbody (kinematic)
    3. On server: go = Instantiate(prefab, pos) on server
    4. On server: NetworkServer.Spawn(go)
    5. On server: go.GetComponent<Rigidbody>().velocity = someDirection
     
  3. dbarrett

    dbarrett

    Joined:
    Sep 6, 2017
    Posts:
    32
    I don't have a problem spawning the missile. If you even looked at my post you would see that I use a command to do everything that you said.