hey @seanr , i have been working with this for a long time…i am firing a bullet prefab as in tanks multiplayer tutorial…but now i also want to add kills and deaths and display it when the player dies(till he respawns)…now i can count the deaths but not kills…i have attached a playerD.cs script to the player which stores his user name , kills , deaths…the codes are given below…i hope you will help
Playerd.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class PlayerD : NetworkBehaviour {
[SyncVar]
public string userName;
public int kills;
public int deaths;
void Start()
{
if (!isLocalPlayer)
return;
}
}
BulletController_net.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class BulletController_net : NetworkBehaviour {
[SerializeField]float shellLifetime=2f;
[SerializeField]bool canKill=false;
[SerializeField]bool isDeathmatch=false;
bool isLive=true;
float age;
MeshRenderer shellRenderer;
public GameObject player;
public bool killed=false;
// Use this for initialization
void Start ()
{
shellRenderer = GetComponent<MeshRenderer> ();
//player = GameObject.FindGameObjectWithTag ("Player");
}
// Update is called once per frame\
[ServerCallback]
void Update ()
{
age += Time.deltaTime;
if (age > shellLifetime)
{
NetworkServer.Destroy (gameObject);
}
}
void OnCollisionEnter(Collision other)
{
if (!isLive)
return;
isLive = false;
//shellRenderer.enabled = false;
if (!isServer)
return;
if (!canKill || other.gameObject.tag != "Player")
return;
if (!isDeathmatch)
{
PlayerHealth health = other.gameObject.GetComponent<PlayerHealth> ();
if (health != null)
killed=health.TakeDamage (1);
if (killed == true)
{
Rpckilled ();
}
}
}
[ClientRpc]
void Rpckilled()
{
PlayerD user = player.GetComponent<PlayerD> ();
user.kills++;
}
}
Shooting_net.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityStandardAssets.CrossPlatformInput;
public class Shooting_net : NetworkBehaviour {
//Drag in the Bullet Emitter from the Component Inspector.
public GameObject Bullet_Emitter;
public ParticleSystem muzzleFlash;
//Drag in the Bullet Prefab from the Component Inspector.
public GameObject Bullet;
//Enter the Speed of the Bullet from the Component Inspector.
public float Bullet_Forward_Force=1000f;
public float waitTilNextFire = 0;
public float fireSpeed = 5;
GameObject Temporary_Bullet_Handler;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (!isLocalPlayer)
return;
Vector3 lookDir = new Vector3(CrossPlatformInputManager.GetAxis ("Horizontal_2"),0,CrossPlatformInputManager.GetAxis ("Vertical_2"));
if (lookDir.magnitude>=1||Input.GetButton("Fire1"))
{
if (waitTilNextFire <= 0)
{
CmdSpawnShell ();
waitTilNextFire = 1;
}
}
waitTilNextFire -= Time.deltaTime * fireSpeed;
}
[Command]
public void CmdSpawnShell ()
{
//The Bullet instantiation happens here.
Temporary_Bullet_Handler = Instantiate (Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
//Sometimes bullets may appear rotated incorrectly due to the way its pivot was set from the original modeling package.
//This is EASILY corrected here, you might have to rotate it from a different axis and or angle based on your particular mesh.
Temporary_Bullet_Handler.transform.Rotate (Vector3.left * 90);
//Retrieve the Rigidbody component from the instantiated Bullet and control it.
Rigidbody Temporary_RigidBody;
Temporary_Bullet_Handler.GetComponent<Rigidbody> ().AddForce (transform.forward * Bullet_Forward_Force);
//Tell the bullet to be "pushed" forward by an amount set by Bullet_Forward_Force.
//Temporary_RigidBody.AddForce (transform.forward * Bullet_Forward_Force);
//Basic Clean Up, set the Bullets to self destruct after 10 Seconds, I am being VERY generous here, normally 3 seconds is plenty.
//Destroy(Temporary_Bullet_Handler, 4.0f);
RpcMuzzle ();
NetworkServer.Spawn(Temporary_Bullet_Handler);
}
[ClientRpc]
void RpcMuzzle ()
{
muzzleFlash.Play ();
Temporary_Bullet_Handler.GetComponent<BulletController_net> ().player = gameObject;
}
}
i have replaced Temporary_Bullet_Handler.GetComponent ().player = gameObject; this in many places but still cant get a reference to who shot the bulllet,but i think now its working for clients every clients not server
thanks in advance