我正在使用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));
}
}
}
}
它在主机上工作正常,因为它在调用命令时有引用我无法弄清楚如何获取客户端的引用。
答案 0 :(得分:0)
@ Dtb49您可以将脚本附加到“missle”对象,而此脚本用于在生成对象的NetworkInstanceId后生成它。
public class YourScript: NetworkBehaviour {
public static NetworkInstanceId nid;
void Start(){
nid = this.netId;
}
}
在其他脚本中,您可以使用NetworkServer.objects [NetworkInstanceId] .gameObject来获取对象引用,如:
if (NetworkServer.objects.ContainsKey (YourScript.nid)) {
Debug.Log (NetworkServer.objects[YourScript.nid].gameObject);
}