Hello.
We are making a ship battle game that’s played over the internet. The first player creates a server that other players can join into. Right now the ships spawn correctly and they move on all clients, but cannon shots only show on the player’s own client. The cannon shots are instantiated objects that have velocity set in relation to the ship it’s fired from. The cannonballs have their own Network Identity with Local Player Authority unchecked. They also have Network Transform with Sync Rigidbody 3D. Cannonball prefab is also registered as spawnable prefab.
Can you help us understand what’s wrong with our code? Is [Command] a good way to make this happen?
The following code is linked to the player that instantiates the object:
void FixedUpdate () {
if (!_identity.isLocalPlayer)
return;
if (Input.GetKey(KeyCode.Q) && Time.time > _nextFire)
{
_nextFire = Time.time + _fireRate;
CmdDoFireV(_missileLifetime);
}
if (Input.GetKey(KeyCode.E) && Time.time > _nextFire2)
{
_nextFire2 = Time.time + _fireRate2;
CmdDoFireO(_missileLifetime);
}
}
private GameObject missile;
private GameObject missile1;
private GameObject missile2;
private GameObject missile3;
private GameObject missile4;
private GameObject missile5;
private GameObject missile6;
private GameObject missile7;
[Command]
void CmdDoFireV(float lifeTime)
{
missile = (GameObject)Instantiate(_missilePrefab, _missileSpawnPoint.position, Quaternion.identity);
missile1 = (GameObject)Instantiate(_missilePrefab, _missileSpawnPoint1.position, Quaternion.identity);
missile2 = (GameObject)Instantiate(_missilePrefab, _missileSpawnPoint2.position, Quaternion.identity);
missile3 = (GameObject)Instantiate(_missilePrefab, _missileSpawnPoint3.position, Quaternion.identity);
Rigidbody rigid = missile.GetComponent<Rigidbody>();
rigid.velocity = ship.GetComponent<Rigidbody>().velocity + (transform.right * -_missileSpeed);
Destroy(missile, lifeTime);
Rigidbody rigid1 = missile1.GetComponent<Rigidbody>();
rigid1.velocity = ship.GetComponent<Rigidbody>().velocity + (transform.right * -_missileSpeed);
Destroy(missile1, lifeTime);
Rigidbody rigid2 = missile2.GetComponent<Rigidbody>();
rigid2.velocity = ship.GetComponent<Rigidbody>().velocity + (transform.right * -_missileSpeed);
Destroy(missile2, lifeTime);
Rigidbody rigid3 = missile3.GetComponent<Rigidbody>();
rigid3.velocity = ship.GetComponent<Rigidbody>().velocity + (transform.right * -_missileSpeed);
Destroy(missile3, lifeTime);
NetworkServer.Spawn(missile);
NetworkServer.Spawn(missile1);
NetworkServer.Spawn(missile2);
NetworkServer.Spawn(missile3);
}
[Command]
void CmdDoFireO(float lifeTime)
{
missile4 = (GameObject)Instantiate(_missilePrefab, _missileSpawnPoint4.position, Quaternion.identity);
missile5 = (GameObject)Instantiate(_missilePrefab, _missileSpawnPoint5.position, Quaternion.identity);
missile6 = (GameObject)Instantiate(_missilePrefab, _missileSpawnPoint6.position, Quaternion.identity);
missile7 = (GameObject)Instantiate(_missilePrefab, _missileSpawnPoint7.position, Quaternion.identity);
Rigidbody rigid = missile4.GetComponent<Rigidbody>();
rigid.velocity = ship.GetComponent<Rigidbody>().velocity + (transform.right * _missileSpeed);
Destroy(missile4, lifeTime);
Rigidbody rigid1 = missile5.GetComponent<Rigidbody>();
rigid1.velocity = ship.GetComponent<Rigidbody>().velocity + (transform.right * _missileSpeed);
Destroy(missile5, lifeTime);
Rigidbody rigid2 = missile6.GetComponent<Rigidbody>();
rigid2.velocity = ship.GetComponent<Rigidbody>().velocity + (transform.right * _missileSpeed);
Destroy(missile6, lifeTime);
Rigidbody rigid3 = missile7.GetComponent<Rigidbody>();
rigid3.velocity = ship.GetComponent<Rigidbody>().velocity + (transform.right * _missileSpeed);
Destroy(missile7, lifeTime);
NetworkServer.Spawn(missile4);
NetworkServer.Spawn(missile5);
NetworkServer.Spawn(missile6);
NetworkServer.Spawn(missile7);
}