Search Unity

Killing score???

Discussion in 'Scripting' started by dick, Jun 25, 2015.

  1. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    heya guys im making a fps multiplayer game and so when a player kills another I have it set up to where the scoreboard I have adds a point to that player saying he died. However, I don't know how I would add a point for a kill?? Please help guys? the killing is done via raycasting...
     
  2. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Uhm. Increment your points counter by 1?
     
  3. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    what do you mean by that? can you explain?
     
  4. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    No, because it was actually meant as a joke. To be honest, I don't quite understand what the question is. You need to provide more information about what's giving you trouble.
     
  5. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    I want to add a score to a scoreboard when I kill another person across network. I already have a way to do this with death, just need one for kill.
     
  6. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    So, where's the problem? You need to add a score, so just add the score, like 1 or 10 or 100? If adding is not the problem, what is?
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how are you handling the "do damage to the thing you hit"? if you need to know "who killed me" just pass the "damage dealing object" as a parameter in the script, so you can do "my death count +1" "damage dealing object kill count + 1" in the "on death" section of your code.
     
  8. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    yea ok ill try to process that @LeftyRight and @ I have a damage system by giving a int of damage. If the player receives the int and is <= 0 then he dies, how would I know WHO killed him?
     
  9. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    heres my health script

    Code (JavaScript):
    1. public class FreddyHealth extends Photon.MonoBehaviour {
    2.  
    3. var freddyhealth : int = 100;
    4. var scream : AudioClip;
    5. var FreddySpot : Transform;
    6.  
    7. @RPC
    8. function ApplyFreddyDamage(damage : int) {
    9. freddyhealth -= damage;
    10.     if( freddyhealth <= 0){
    11.     FreddyDie();
    12. }
    13. }
    14.  
    15. function FreddyDie() {
    16. if( photonView.isMine ) {
    17.     if( gameObject.tag == "Freddy" ) {
    18. GameObject.Find("Score").SendMessage("FreddyDeath");
    19. PhotonNetwork.Instantiate("FREDDYDEAD", FreddySpot.transform.position, FreddySpot.transform.rotation, 0);  
    20. PhotonNetwork.Destroy(gameObject);
    21. }
    22. }
    23. }
    24. }
     
  10. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    btw damage is done via raycast
     
  11. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    @LeftyRighty can you please give me an example? im confused
     
  12. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I think he is saying to add in your "ApplyFreddyDamage" method a second parameter such as a reference to the player that is dealing the damage and store it in a variable such as "lastPersonToHitMe". Then in FreddyDie, you get the lastpersontohitme variable and add a point to his score.
     
  13. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    oh ok ill try it, and for the lastPersonHitMe should the variable be a GameObject?
     
  14. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    as @HiddenMonk says, at the moment you have a single parameter, the amount of damage done. Your problem is that when "freddy" dies, you need to know what did that damage. Solution, add a parameter to pass that information along. So instead of "do 5 damage" you say "do 5 damage from BigMeanie", you can then use that parameter when you need to say "BigMeanie scored a kill".

    Yes, gameobject, or whatever you base "unit"/"entity" class is if you have something specific setup.
     
    Last edited: Jun 25, 2015
  15. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    oh ok thanks ill try
     
  16. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    @LeftyRighty im sorry but could you give me an example of how I would do that?
     
  17. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    its not fullcode, but

    Code (csharp):
    1.  
    2. function ApplyFreddyDamage(damage : int, damager : GameObject)
    3. {
    4. ...
    5. FreddieDie(damager);
    6. ...
    7. }
    8.  
    9. function FreddyDie(damager : GameObject)
    10. {
    11. ...
    12. GameObject.Find("Score").SendMessage("FreddyDeath", damager);
    13. ...
    14. }
    15.  
     
  18. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    oh ok. When I try to hit him, nothing happens, also this is the code another player has to inflict damage on him, what would I need to add in the parameters for this RPC to affect the 'ApplyFreddyDamage' function Above ^ ::
    Code (JavaScript):
    1. if (hit.transform.tag == "Freddy") {
    2.             var targetID = hit.collider.gameObject.GetComponent(PhotonView);
    3.             targetID.photonView.RPC("ApplyFreddyDamage", PhotonTargets.AllBuffered, damage);
    4.          
    5.             }
     
  19. Topherpunch

    Topherpunch

    Joined:
    Mar 5, 2013
    Posts:
    8
    Hey Lefty, just add a counter in the if statement that checks if his health is 0. So....
    Code (CSharp):
    1. function ApplyFreddyDamage(damage : int) {
    2. freddyhealth -= damage;
    3.     if( freddyhealth <= 0){
    4. //new varible here that increments everytime freddy has less than zero hp
    5. freddyScore ++;
    6.     FreddyDie();
    7. }

    Really it is just that simple.
     
  20. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
  21. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    @Topherpunch that's counting deaths, OP is looking for passing killer's kills.
     
  22. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    @LeftyRighty is there any new parameter I need to add in the new script I just added?
     
  23. dick

    dick

    Joined:
    Sep 6, 2014
    Posts:
    91
    @LeftyRighty I get an exception error saying that the code I use to inflict damage on freddy has only 1 parameter. what do I need to add on the other one.
     
  24. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    If youre using leftys example then you need to add a gameobject parameter (the gameobject that is shooting). Then you know what gameobject did the kill, however, maybe you would prefer to pass a NetworkViewId parameter instead.

    All we have really done is give you an idea of how you could know who killed who.
    How you assign the points is really up to how you have things set up. In your freddydie method you would probably send another message to the scoreboard telling it to add a point to the owner of the networkid or gameobject.

    Im not sure if you are a beginner, but if you are, multiplayer might not be a great starting point. I have yet to touch multiplayer stuff. Its just an extra overhead to the code design you are doing, which might be confusing for a beginner.
     
  25. M4R5

    M4R5

    Joined:
    Apr 11, 2013
    Posts:
    33
    RPC's don't take Transforms or Gameobjects by default. Try giving your Damage function an additional parameter (int) and set it to the PhotonView.UserID of the attacker. Then let the defender (the one executing the Damage function) run a PhotonView.Find() and pass along the ID as such:


    void ExampleShoot ()
    {
    defender.transform.GetComponent<PhotonView>().RPC("ExampleDamage", PhotonTargets.All, damage, attacker.GetComponent<PhotonView>().viewID);
    }

    [PunRPC]
    public void ExampleDamage (float damage, int attackerID)
    {
    if ((Health - damage) <= 0)
    {
    Transform attacker= PhotonView.Find(attackerID).transform;
    attacker.Kills++; defender.Deaths++;
    //Respawn
    }
    else
    {
    Health -= damage;
    }
    }
     
  26. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    You need to resolve who did the damage, we do it like this in our game
    Code (CSharp):
    1. public class NetworkedFirearm : NetworkTrackedObject, IRequireInitialSync
    2.  
    3.    {
    4. protected bool ApplyDamage(ulong avatarId, int hitZoneId, float damage, Vector3 point)
    5.        {
    6.            var hitZone = GetHitZone(avatarId, hitZoneId);
    7.            hitZone.RegisterHit(OwnerId, LocalFirearm.BloodOnImpactPrefab, LocalFirearm.FlashPoint.transform.forward, point, LocalFirearm.FlashPoint.transform.rotation, damage);
    8.  
    9.            return hitZone.Avatar.Health < 0.01f;
    10.        }
    Basicly we supply the RegisterHit method with the OwnerId of the firearm who did the damage.

    Anyway further down the chain this publishes a event

    Code (CSharp):
    1. EventBus.Instance.Publish(new PlayerKilled(killerId, OwnerId, shouldAffectScore));
    This is picked up by the Scoring system and updates score accordingly