I want to call PunRPC only to a specific player

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.

I want to call PunRPC only to a specific player

ChoChick
2020-05-12 15:10:47

There are All and Other targets for PunRPC, but is there a way to send them only to the player with a specific ViewID?
Is there no choice but to send the ViewID as a PunRPC parameter, check the ViewID on the receiving side, and ignore the processing other than the corresponding ViewID?

Comments

ChoChick
2020-05-13 01:44:40

PhotonNetwork.RPC (string methodName, Player targetPlayer, params object [] parameters)

It turns out that you can send an RPC to a specific player by specifying Player.
But I don't know how to identify the player in front of me.

Tobias
2020-05-13 07:55:44

The object in front of you might have a PhotonView. If so, the PhotonView knows the Controller and Owner. If you don't transfer ownership of objects, the Owner is what you are looking for.

ChoChick
2020-05-13 10:12:21

I was able to get the Player with PhotonView.Controller.
I was able to send PunRPC only to the target.
Thank you.

DaWeege
2022-02-21 17:45:38

ChoChick 2020-05-13T10:12:21+00:00
I was able to get the Player with PhotonView.Controller.

I was able to send PunRPC only to the target.

Thank you. Is there anyway you could go into more detail on how you did this? I'm trying to target just one player but can't seem to figure it out

Kikinda
2022-08-31 21:51:30

DaWeege 2022-02-21T17:45:38+00:00

https://forum.photonengine.com/discussion/comment/50955#Comment_50955

Is there anyway you could go into more detail on how you did this? I'm trying to target just one player but can't seem to figure it out

Hello, I know I am late, but this may help other people with similar issues.

So I went through the whole documentation and a lot of forum posts but no one talks about this.

So basically you can send RPC either to RpcTarget (all, other...) or a Player(now this I didn't find in the documentation and this is what the post is about). To send to a specific player you need to replace the RpcTarget.all/RpcTarget.others (or other) with a VARIABLE.Controller. Now you ask yourself what this VARIABLE is, it is the PhotonView component of the player GameObject that you want to send the RPC to. Here is my example:

As you can see I raycasted to a player and then I got the PhotonView component from that GameObject that I hit. Then When I call the RPC in the last row instead of putting RpcTarget.all/RpcTarget.others (or other) I put playerHitView.Controller.

This is hot to send RPC to one specific player. I think this needs to be easier to find in the documentation since it is a big thing and a lot of users need it.

tleylan
2022-09-02 17:03:33

I'm late to the party but... I really like that you shared a solution (I wish more people would). I would ask that you consider adding it as "code" rather than an image though. It is impossible to copy the solution and I'd rather not key it all in to reply :-)

A few aspects could be improved. You get hit.transform.GetComponent() within your if test and then if it isn't null you go get it again. Consider assigning it to a variable the first time and checking if that variable is null and there is no need to get it second time.

I don't know why you check playerHitScript (probably a reason) but if you are not going to call the RPC if it is null then there was now reason to do most of the lines above it. Finally if seems that the playerHitView might not be assigned but still used for the RPC call. If the view is null you would not have assigned it.

Don't mean to sound like I'm ragging on your but attention to detail is the key to keeping code organized. Hope this helps :-)

Kikinda
2022-10-27 20:37:13

I was new to the site so I didn't know how to post code so I posted the image.

The code was messy because I have written it in a minute later that week I fixed it by adding a variable for hit.transform.GetComponent() and I checked the playerHitView in the same if as hit.transform.GetComponent().

Back to top