- Home /
Network Instaniate,Lag,Bullet Prefab
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);
}
Since FireBullet
only gets called on the server, what would be the point in sending an RPC with Log
to oneself, ins$$anonymous$$d of just using Debug.Log
?
You should also cache your finds/component gets. Not only will it make your code much much faster, it will also make it much cleaner and easier to read.
well im using unity 2.5, so some of the code is probably outdated
The unity version had nothing to do with my suggestions, using an outdated version does not excuse writing bad code.
sorry, but most of the code that i was given was done by my tutor. im just using his framework to make my game work
The code looks like it should work. Have you tried debugging it yourself? Does it throw any errors?
Answer by Mint92 · Apr 27, 2014 at 12:08 PM
Okay so i was doing abit of research, and i didnt have "Run in Background" check under the player settings. Now that the bullet seems to spawn from the client side, and i can see, it gets deleted very quickly.
Your answer
Follow this Question
Related Questions
RPC Call Mix Up Issues 0 Answers
Get RPC senders ID, or IP? 1 Answer
Network RPC - exclude specific client 1 Answer
client to client ping - NAT punchthrough 1 Answer
Spawning Clients 1 Answer