我正在使用 UNET 制作游戏并为 HoloLens 制作游戏。基本上,我的游戏非常简单,玩家加入会话,然后他们可以生成一艘船来使用 Xbox 控制器进行控制。我有移动机制,甚至有一个基本的射击功能。接下来我真正想添加的是类似于寻的导弹。我有寻的导弹的代码,它在主机上运行良好,但是,如果有意义的话,我不知道如何将我的导弹对象信息返回给玩家。我会告诉你我的意思。
我的命令:
[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));
}
}
}
}
它在主机上运行良好,因为它在调用命令时具有引用我只是不知道如何获取客户端的引用。