I want to call PunRPC only to a specific player
The whole answer can be found below.
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).
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
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.
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.
I was able to get the Player with PhotonView.Controller.
I was able to send PunRPC only to the target.
Thank you.
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
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.
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
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 :-)
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