Hey guys, when i play the game as the server, the bullet fires like it should without any problems, however when i fire the bullet on the client side, nothing shows up and when click back onto the server, you can just see the bullet firing. I dont understand why its not updating properly on the client side.
void Update ()
{
if (game.running && Network.player == owner)
{
playerActive = true;
bool shootButton;
if (Input.GetKey("space"))
{
shootButton = true;
}
else
{
shootButton = false;
}
if (shootButton == true && Time.time > nextFire)
{
Debug.Log("Firing!");
nextFire = Time.time + reloadTime;
Transform turret = transform.Find("TurretPivotControl");
TurretControl turretScript = (TurretControl)turret.GetComponent("TurretControl");
if (Network.isServer)
{
Debug.Log("Server is spawning Bullet" + bulletCount + " fired by " + gameObject.name);
// call FireBullet RPC in clients
networkView.RPC("FireBullet", RPCMode.Others, gameObject.name, "Bullet"+bulletCount, transform.Find("TurretPivotControl/Turret/BulletSpawnPoint").transform.position, turret.transform.rotation, turretScript.getForce());
// create bullet on local server
GameObject bullet = (GameObject)Instantiate(bulletPrefab, transform.Find("TurretPivotControl/Turret/BulletSpawnPoint").transform.position, turret.transform.rotation);
bullet.name = "Bullet"+bulletCount;
((Bullet)bullet.GetComponent("Bullet")).setFiredBy(gameObject.name);
// add bullet to server list of bullets
game.addBullet(bullet);
bulletCount++;
bullet.rigidbody.AddForce(turret.right * turretScript.getForce());
}
else
{
networkView.RPC("Log", RPCMode.Server, "Client has fired Bullet"+bulletCount);
// call server FireBullet RPC
networkView.RPC("ServerFireBullet", RPCMode.Server, gameObject.name, transform.Find("TurretPivotControl/Turret/BulletSpawnPoint").transform.position, turret.transform.rotation, turretScript.getForce());
}
}
}
}
I then call it through the RPC
[RPC]
public void ServerFireBullet(string shipName, Vector3 position, Quaternion rotation, float force)
{
if (Network.isServer)
{
Debug.Log("Server ordered to spawn Bullet" + bulletCount + " fired by ship " + shipName);
// tell other clients that the bullet was fired
networkView.RPC("FireBullet", RPCMode.Others, shipName, "Bullet"+bulletCount, position, rotation, force);
// find the local server version of the ship
GameObject ship = GameObject.Find(shipName);
Transform turret = ship.transform.Find("TurretPivotControl");
// create local server version of bullet
GameObject bullet = (GameObject)Instantiate(bulletPrefab, ship.transform.position, bulletPrefab.transform.rotation);
bullet.name = "Bullet"+bulletCount;
((Bullet)bullet.GetComponent("Bullet")).setFiredBy(shipName);
Game game = (Game)GameObject.Find("GameManager").GetComponent("Game");
// add bullet to server list of bullets
game.addBullet(bullet);
bulletCount++;
bullet.rigidbody.AddForce(turret.right * force);
}
}
[RPC]
public void FireBullet(string shipName, string bulletName, Vector3 position, Quaternion rotation, float force)
{
networkView.RPC("Log", RPCMode.Server, "Client ordered to spawn " + bulletName + " fired by ship " + shipName);
// find local client version of ship
GameObject ship = GameObject.Find(shipName);
Transform turret = ship.transform.Find("TurretPivotControl");
networkView.RPC("Log", RPCMode.Server, bulletName + " fired by " + ship.name + " at " + ship.transform.position);
// create local client version of bullet
GameObject bullet = (GameObject)Instantiate(bulletPrefab, ship.transform.position, bulletPrefab.transform.rotation);
bullet.name = bulletName;
((Bullet)bullet.GetComponent("Bullet")).setFiredBy(shipName);
bullet.rigidbody.AddForce(turret.right * force);
}