- Home /
Question by
Mint92 · Apr 29, 2014 at 12:27 PM ·
networkbulletturn-based
Help with turn based bullet fire
void Update ()
{
if (game.running && Network.player == owner)
{
playerActive = true;
bool shootButton;
//Move the player left and right using the arrow keys
if(Input.GetButtonDown("Right"))
{
transform.position = new Vector3(transform.position.x + 1.0f, transform.position.y,transform.position.z);
}
else if(Input.GetButtonDown("Left"))
{
transform.position = new Vector3(transform.position.x -1.0f, transform.position.y,transform.position.z);
}
if (Input.GetKey("space"))
{
shootButton = true;
}
else
{
shootButton = false;
}
//Call SetPosition to notifty the client is moving
if(Vector3.Distance(transform.position,lastPosition) <= 1)
{
networkView.RPC("Log", RPCMode.Server, "Client " + owner + " is moving");
lastPosition = transform.position;
networkView.RPC("SetPosition", RPCMode.Others, gameObject.name, transform.position);
}
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());
}
}
}
}
this is my script that handles that spawns the bullet, but im abit stuck on how to make it turned based so after each player has fired 1 bullet, it switches.
i have another script,called connection which handles the player ID
Comment
I would suggest some kind of manager game object, that is independant from each player. Each time a player has made his move (like a shoot like you say), he tells the manager that he played.
The manager on its side must give the hand on one player at a time. It needs to know each player, and activate only one player at a time. Each time a player has played his move, the manager knows it (by having a specific variable valued), deactivate the player who just played, and activate the next one.