I am firing a missile on key down using:
networkView.RPC("SpawnMissileRequest", RPCMode.Server, transform.position + transform.forward * 0.7f, transform.rotation, transform.forward * speed);
The server then sends an RPC to all clients to instantiate this missile:
[RPC]
void SpawnMissileRequest(Vector3 position, Quaternion rotation, Vector3 force)
{
NetworkViewID nView = Network.AllocateViewID();
networkView.RPC("SpawnMissile", RPCMode.All, position, rotation, force, nView);
}
The clients then create the missile:
[RPC]
void SpawnMissile(Vector3 position, Quaternion rotation, Vector3 force, NetworkViewID nVid)
{
Rigidbody instantiatedProjectile = (Rigidbody)Instantiate(projectile, position, rotation);
NetworkView nView = (NetworkView)instantiatedProjectile.GetComponent(typeof(NetworkView));
nView.viewID = nVid;
if (Network.isServer){
instantiatedProjectile.AddForce(force, ForceMode.Impulse);
}
CapsuleCollider parentCollider = (CapsuleCollider)playerBody.GetComponent(typeof(CapsuleCollider));
BoxCollider missileCollider = (BoxCollider)instantiatedProjectile.GetComponent(typeof(BoxCollider));
Physics.IgnoreCollision(parentCollider, missileCollider);
}
And here is how I am handling the damage when it collides and the “DestroyRocket” RPC is called from the server side to all clients, therefore the clients are calculating the damage for themselves:
[RPC]
void DestroyRocket(Vector3 explosionPosition, Quaternion rotation){
GameObject instantiatedExplosion = (GameObject)Instantiate (explosion, explosionPosition, rotation);
Collider[] colliders = Physics.OverlapSphere (explosionPosition, explosionRadius);
foreach (Collider hit in colliders) {
if (hit.rigidbody) {
hit.rigidbody.AddExplosionForce (explosionPower, explosionPosition, explosionRadius, 3.0f);
}
Vector3 closestPoint = hit.ClosestPointOnBounds (explosionPosition);
float distance = Vector3.Distance (closestPoint, explosionPosition);
float damage = 0;
if (distance < explosionRadius) {
if (distance == 0)
distance = 1;
damage = Mathf.Floor(explosionDamage / Mathf.Ceil (distance));
}
if (hit.collider.gameObject.tag == "Player" hit.collider.gameObject.networkView.isMine) {
Player playerHealth = (Player)hit.GetComponent("Player");
playerHealth.SendMessage("ApplyDamageToPlayer", damage, SendMessageOptions.DontRequireReceiver);
}
}
Destroy(gameObject);
}
My question is, when the missile collides with a player, how can I get a reference to the “shooter” of the missile so that I can add a certain amount of points to the player that fired the missile?