- Home /
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).
Answer by Arkaic · Jun 02, 2014 at 05:45 AM
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. https://www.youtube.com/watch?v=AIgwZK151-A&list=PLbghT7MmckI7BDIGqNl_TgizCpJiXy0n9
Hope this helped and wasnt to confusing, i can explain better if need.
Good Luck, Arkaic.
By the way, this is all in Javascript which im sure you noticed lol. I can translate if needed.
What you explain there is more or less what I did (I'm not sure if I understood it correctly). So what you do is a PhotonNetwork.instantiate and then when the collision is done you target everyone? (is that what photontargets.all does right?). Wouldn't that damage everyone?
edit: I guess i'm not understanding well how rpc works yet :/
What I really don't understand is why is the first RPC message sent? When I click to shoot is like sending a message to myself and then to the target hit when the bullet gets there, I don't understand why this happens.
What im doing is telling everyone through a rpc call to create the bullet/bullets with a normal unity function Instantiate.
The PhotonTargets.All tells everyone to do that function yes. But it tells everyone to do the function to the specific photonview id that the call was made on. I would strongly suggest watching the tutorials i linked you. It will be a huge help in what your trying to accomplish. Just dont actually destroy the players like he $$anonymous$$ches.
Networking is a rather complicated thing and takes a let of toying with to understand completely. Im sorry i couldnt be more of a help.
Good Luck, Arkaic.
Those videos helped me out to figure what was going on, thanks!