How to give experience points to player

How would you get the, I guess the id of a player, who has fired a projectile and destroyed another networked player and give experience points to the destroying player?

I've tried a couple different things but haven't gotten it right.

public int xP = 10;

addXp();

void addXp(int Amount, int DestroyerID ) {
xP += Amount;
Debug.Log("Destroyer : Add XP");
PhotonPlayer Destroyer = PhotonPlayer.Find (DestroyerID);
Destroyer.gameObject.GetComponent<ExperienceManagerC>().AddXP(Amount);
}

ExperienceManagerC function:

public void AddXP ( int Amount ){
XP += Amount;
}

Comments

  • This depends on how you implement projectile and hit detection. Projectile itself may have reference to player or player id.

  • Firing

    if(Input.GetButtonDown("Fire2"))
    {
    GetComponent<PhotonView>().RPC ("ShootFireball", PhotonTargets.All);
    cooldown = fireRate;
    audio.PlayOneShot(fireballSound);
    }

    }

    [RPC]
    private void ShootFireball()
    {

    Rigidbody clone;
    clone = Instantiate(Fireball, Mouth.position, Mouth.rotation) as Rigidbody;
    clone.rigidbody.AddForce(transform.forward * Force);

    }

    Hit Detection
    using UnityEngine;
    using System.Collections;

    public class FireballDamage : MonoBehaviour {

    public int Power = 5;

    public GameObject explosion;
    public AudioClip explosionSound;
    public GameObject Fireball;

    void OnCollisionEnter(Collision col)
    {
    col.gameObject.BroadcastMessage("TakeDamage", Power, SendMessageOptions.DontRequireReceiver);
    Instantiate (explosion, transform.position, transform.rotation);
    AudioSource.PlayClipAtPoint (explosionSound, transform.position,1);
    Destroy (Fireball.gameObject);
    }

    another issue

    Networking the ridgidbody projectile is not right also, I think. I'm having a hard time finding good references for this.
  • Make fireball PhotonView object and instantiate it with PhotonNetwork.Instantiate() (no ShootFireball RPC needed). Detect collision on fireball owner only and notify about hit others (add Hit RPC to fireball script). That would fix hit mismatch on different clients. Also you will get attacker id automatically in hit handler.
    Note: if possible, avoid synchronizing fireball position. Better calculate it each frame on every client having same initial position and velocity.