[Solved] Trouble Sending Combat Damage w/ RPCs

Hi there!

I'm having what I think is a fairly basic issue with RPCs; I've got a scene lain out and am able to spawn multiple players in it, and keep their positions and animations synchronized on all screens using Photon's serialize function. I'm now at a point where I want to script damage between the players, though, and despite trying to (roughly) follow this Unity-FPS tutorial, the best I've managed to achieve (after a few days of tinkering and trying out 5-6 different implementations) is for each of the players to individually register the damage dealt and done while their particular character is controlled; damage dealt in the Unity editor copy of the game doesn't seem to register on the Webplayer copy, and vice versa.

Each player is a PhotonNetwork.Instantiated prefab; it contains the primary prefab objects (Unity's first-person character controller with the capsule graphic removed), and as children: A) a mesh for graphics (built in Blender; in turn it has 5-6 child meshes), B) a camera object, and C) a GuiText object (to display a name label above the player's avatar). The root gameobject of the prefab has a PhotonView script component; the graphics-mesh object also has a PhotonView script.

To try and deal damage, the animations script on the graphics-mesh object calls the following inside its Update() call, to detect that the player is attacking and play the proper animation. If the Raycast-attack hits a PlayerPrefab object, the script then tries to SendMessage to the Combat script, to call its takeDamage function, with the appropriate amount of damage indicated. The takeDamage function of the Combat script then calls an RPC to change the health amounts and rendered health bars of the player who was struck, for all players---or, that's what I want it to do, anyway! Instead, in this current version, neither player's health bar ever moves. I have had an alternative version (that used a Trigger Collider rather than Raycasting to detect melee damage) in which both player's health bars would move, but only in response to damage dealt/received on their own copy of the game.
// Update is called once per frame

    void Update () {

        if(Input.GetButtonDown("Fire1") && AttackCounter == 0 && hasAx == false)

        {

            animation.Play("Attack_Arm");

            curAnim = "Attack_Arm";

            AttackCounter += 1;

            Ray ray = new Ray(Camera.main.transform.position + 2f*Vector3.forward, Camera.main.transform.forward);

            RaycastHit hitInfo;

 

            if(Physics.Raycast (ray, out hitInfo, armRange))

            {

                Debug.Log ("We arm-hit: " + hitInfo.collider.name);

                if(hitInfo.collider.name == "PlayerPrefab")

                {

                    print("Trying to hurt " + hitInfo.collider.transform.name + " by calling script " + hitInfo.collider.transform.FindChild("ax_troll_3d_run").GetComponent<Combat>().name);

                    hitInfo.collider.transform.FindChild("ax_troll_3d_run").GetComponent<Combat>().SendMessage ("takeDamage", armDmg);

                }

            }

        }

else

{

... stuff ...

 

}
using UnityEngine;

using System.Collections;

 

public class Combat : MonoBehaviour {

 

    public float maxHealth = 100f;

    public float curHealth = 100f;

    public HealthBarShader hbs;

 

    // Use this for initialization

    void Start () {

        hbs = (HealthBarShader)transform.parent.FindChild ("Health").GetComponent<HealthBarShader>();

    }

 

    void takeDamage(float dmgAmt)

    {

        transform.GetComponent<PhotonView>().RPC("changeHealth", PhotonTargets.AllBuffered, dmgAmt);

    }

 

    [RPC]

    void changeHealth(float dmgAmt)

    {

        curHealth -= 10f;

        hbs = (HealthBarShader)transform.parent.FindChild ("Health").GetComponent<HealthBarShader>();

    }

}

I'm sure there are lots of problems with how I'm approaching this; any pointers on good documentation to read about RPCs, or just tips for how I should modify my RPC call to work properly? Or obvious blunders I've made that needing pointing out?

Thanks!

Comments

  • Nevermind! I got the RPC call working! (Well, mostly --- now, when one player hits the other player, the player who did the hitting gets hurt, haha. But he gets hurt on all screens, so I'm counting that as a win! Getting it to hit the proper player should be easy now, hopefully!)

    Seems I had it written correctly, but while hurriedly trying to fix it, managed to screw up a number of other things!
  • Hehe. Sounds like you are making some progress now.
    Keep up the experimentation!
Sign In or Register to comment.