Bullet damage to other player with raycasting. Answer fast please(

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Bullet damage to other player with raycasting. Answer fast please(

twonkykong
2020-10-28 08:48:25

So, I made a gun with raycasting and I don't know how to apply damage to player I'm looking at. Just like: if (Physics.Raycast(...)) { if (hit.collider.tag == "Player") hit.collider.GetComponent().hp -= 10; }
But how to do it over PUN2?

Comments

S_Oliver
2020-10-29 07:09:53

Sending an RPC. If you don't know what this is check out https://doc.photonengine.com/en-us/pun/v2/demos-and-tutorials/pun-basics-tutorial/intro.

twonkykong
2020-10-29 07:21:07

@S_Oliver wrote: »

Sending an RPC.

ye i already got it.
For thoose who went here for help:

void Update()
{
RaycastHit hit;
if (Physics.Raycast(head.transform.position, transform.forward, out hit, 50f))
{
if (hit.collider.tag == "Player")
{
this.photonView.RPC("GetDamage", RpcTarget.All, hit.collider.GetComponent().UserID);
}
}
}

[PunRPC]
public void GetDamage(string userid)
{
if (PhotonNetwork.LocalPlayer.Owner.UserID == userid)
{
GetComponent().hp -= 30;
}
}

i think i wrote something wrong but the main idea is that we have method which we give a player's user id that we looking at
then the method on all players' clients checks, if one of player has the same userid as given one
if true: get damaged

hope you understand, it is so simple for me. I was thinking about it 1 day

Back to top