//shoots a bullet in the direction passed in //we do not rely on the current turret rotation here, because we send the direction //along with the shot request to the server to absolutely ensure a synced shot position protected void Shoot(Vector2 direction = default(Vector2)) { //if shot delay is over if (Time.time > nextFire) { //set next shot timestamp nextFire = Time.time + fireRate; //create bullet from prefab GameObject bullet = (GameObject)Instantiate(bulletPrefab, shotPos.position, turret.rotation); //add velocity to bullet bullet.GetComponent <Rigidbody>().velocity = bullet.transform.forward * 6.0f; //bulletSpawn bullet on client NetworkServer.Spawn(bullet); //destory bullet after 2 sec Destroy(bullet, 2); //spawn bullet using pooling, locally // GameObject obj = PoolManager.Spawn (bullet, shotPos.position, turret.rotation); NetworkedBullet blt = bullet.GetComponent <NetworkedBullet>(); blt.owner = gameObject; if (shotFX) { PoolManager.Spawn(shotFX, shotPos.position, Quaternion.identity); } if (shotClip) { AudioManager.Play3D(shotClip, shotPos.position, 0.1f); } } }
/// <summary> /// Calculate damage to be taken by the Player, /// triggers score increase and respawn workflow on death. /// </summary> public void TakeDamage(NetworkedBullet bullet) { if (!isServer) { return; } //substract health by damage health -= bullet.damage; // OnHealthChange (health); //bullet killed the player if (health <= 0) { if (isLocalPlayer) { winLose.text = "Lose"; } RpcPlayerDead(); gameObject.SetActive(false); } }