当游戏在客户端[本地服务器]上开始时,子弹不会在空间中向前移动。他们只是固定在枪的位置。请参阅下图以了解问题的说明:
Network Manegar: https://imgur.com/mfcBo61
Player: https://imgur.com/DuOIy67
Bullet: https://imgur.com/cVq5HFU
这是我的脚本
void Update()
{
if (!isLocalPlayer)
{
return;
}
if (Input.GetKey(KeyCode.Mouse0))
{
CmdBulletFire();
}
}
[Command]
void CmdBulletFire()
{
// create the bullet prefab
GameObject bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
//add velocity to bullet
bullet.GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.forward * 50000f));
//Spawn the bullet
NetworkServer.Spawn(bullet);
//Destroy the bullet
Destroy(bullet, 5f);
}
问题可能是什么,如何解决?
仍然是问题:(
最佳答案
在更新函数中,在调用 CmdBulletFire()
之前,您必须确保这是 isLocalPlayer
。
您的新更新功能:
void Update()
{
if (!isLocalPlayer)
{
return;
}
if (Input.GetKey(KeyCode.Mouse0))
{
CmdBulletFire();
}
}
最后,请确保执行以下操作:
1.将 NetworkTransform
和 NetworkIdentity
组件附加到预制件。
2.将预制件注册到NetworkManager:
关于c# - 子弹不会向前移动,而是停留在枪的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50239549/