简体   繁体   中英

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

I am making a game using UNET and making it for the HoloLens. Basically, my game is pretty simple, players join the session and then they can spawn a ship to control with an Xbox controller. I have the move mechanics and even a basic shoot function working. What I really want to add next is something like a homing missile. I have the code for the homing missile and it works fine on the host but, I can't figure out how to get my missile object information back to the player if that makes sense. I will show you what I mean.

My Command:

[Command]
    public void CmdFireMissle()
    {
        Vector3 bulletDir = planeToSpawn.transform.forward;
        Vector3 bulletPos = planeToSpawn.transform.position + (bulletDir * (0.01f + 3 * planeToSpawn.transform.localScale.x));

        // The bullet needs to be transformed relative to the shared anchor.
        missleToSpawn = Instantiate(missle, sharedWorldAnchorTransform.InverseTransformPoint(bulletPos), Quaternion.LookRotation(bulletDir));
        missleToSpawn.transform.localScale = planeToSpawn.transform.localScale * 0.1f;
        missleToSpawn.GetComponentInChildren<Rigidbody>().velocity = bulletDir * 1.0f;
        NetworkServer.Spawn(missleToSpawn);

        RpcPlayBulletAudio(planeToSpawn);

        // Clean up the bullet in 15 seconds.
        Destroy(missleToSpawn, 15.0f);
    }    

Update Method:

void Update()
{

 if (controllerInput.GetButtonDown(ControllerButton.A) && planeSpawned )
        {
            if (isLocalPlayer)
            {
                bool raycastHit = Physics.Raycast(transform.position, direction: transform.forward, hitInfo: out hit, maxDistance: range);
                if (raycastHit && hit.transform.gameObject.CompareTag("Plane"))
                {
                    enemyShip = hit.transform.gameObject;

                    CmdFireMissle();
// I need to get a reference to my missleToSpawn object on my client
// So I can use it here in this coroutine
                    StartCoroutine(MoveTo(missleToSpawn, misslePos, enemyShip, 1));
                }
            }
        }
}

It works fine on the host since it has the reference when it calls the Command I just can't figure out how to get the reference for the client.

@Dtb49 you can attach a script to the "missle" object, while this script is used to get the spawn object's NetworkInstanceId once it's spawned.

public class YourScript: NetworkBehaviour {
    public static NetworkInstanceId nid; 

    void Start(){
        nid = this.netId;
    }
}

In other script, you can get the object reference by using NetworkServer.objects[NetworkInstanceId].gameObject like:

if (NetworkServer.objects.ContainsKey (YourScript.nid)) {
        Debug.Log (NetworkServer.objects[YourScript.nid].gameObject);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM