Photon shot collision

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).

I waited a while to see if anyone one with more knowledge then me would reply but I’ll give it a shot :p.

What I have done for bullets is I used the RPC function to tell everyone to create the bullets with the normal Instantiate function. The reason for that is it made deleting the objects sooo much simpler.

I used a health script that i attached to players then attached a damage script to the bullet prefab. In the bullet prefab under function OnCollisionEnter(collision : Collision) I checked

if(collision.transform.tag == "Enemy"){ collision.transform.GetComponent(PhotonView).RPC("TakeDamage", PhotonTargets.All, damage); }

The RPC that im calling in that script is a RPC function on the healthscript

@RPC

function TakeDamage(amt : float){
health -= amt;
}
}

The rpc calls from what i understand always finds the right photon view id so it auto applies the damage to the right player on each game client.

In return you are able to tell everyone to create the bullet in the right position then from there when it accomplishes the things you are checking for you tell make it do a RPC that tells everyone to do what you want.

This method works very well for telling players to create particles/explosions things like that.

I would suggest watching through Quill18Creates multiplayer fps tutorial playslist.

Hope this helped and wasnt to confusing, i can explain better if need.

Good Luck, Arkaic.