0
我使用UNET製作遊戲並製作HoloLens。基本上,我的遊戲非常簡單,玩家加入會話,然後他們可以派生出一艘船來用Xbox控制器進行控制。我有移動機制,甚至是基本的拍攝功能。接下來我想增加的東西就像是一枚尋的導彈。我擁有尋的導彈的編碼,它在主機上工作正常,但是,我無法弄清楚如果有意義的話,我的導彈對象信息返回給玩家。我會告訴你我的意思。如何使用Unity的Unet從服務器獲取gameObject的實例到客戶端?
我的命令:
[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);
}
更新方法:
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));
}
}
}
}
它正常工作在主機上,因爲它時,它調用命令我只是無法弄清楚如何獲得參考爲客戶提供參考。