Hi everyone!
I’m trying to instantiate a dodgeable shot on a multiplayer game test im trying. So far I got this:
The one who shots:
public class movement : MonoBehaviour {
/..code............../
if (Input.GetMouseButtonDown(0)) {
GameObject g = PhotonNetwork.Instantiate("shot", transform.position, Quaternion.identity, 0);
/...more code......./
[RPC]
void Applydamage(float damage, PhotonMessageInfo info)
{
health -= damage;
}
}
Here I basically instantiate the shot and set the target of it.
The shot code when it collides with the other player:
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag != "me") {
coll.gameObject.GetComponent<PhotonView>().RPC("Applydamage", coll.gameObject.GetComponent<PhotonView>().owner, damage);
if (this.gameObject.GetPhotonView().isMine) PhotonNetwork.Destroy(this.gameObject);
}
}
I want to point out that when I instantiate a player, I set a “me” tag to make shots only hit others. The problem is that, when I instantiate a shot, the one who shots always gets damaged (like if that applydamage function is called always). The damage done to the other ones is applied correctly, only if hit. What am I doing wrong?
Thanks for your help!
Edit: The one who shots is the one losing health always. When I instantiate the shot the applydamage function is called almost instantly for the one shooting.
Edit2: Looks like it just happens when the shooter is not moving. The function is called out of nowhere (I’m controlling the only rpc call I made and its not even entering on that if so I dont understand what’s going on, if someone wants to see the full code or can purpose a simpler way to get this working i’d appreciate it a lot!
Edit3: I think it has something to do when it collider with itself, when moving the shots usually don’t collide with you, but when standing still they always do. But I still don’t get why, there’s only one RPC call and a I use a Debug.log to know when its called, and it is not when standing still. I got something like:
Debug.log("in2");
coll.gameObject.GetComponent<PhotonView>().RPC("Applydamage", coll.gameObject.GetComponent<PhotonView>().owner, damage);
And that “in2” is not showing on the console, how can the message be sent then?
Edit4: No matter what the damage dealt is always 1.
Edit5: I get one call to the Applydamage function for each other player in the scene, looks like its called from nowhere and then the parameter is always 1 (for some reason).