[SOLVED]How to get target for RPC

Hello everybody!

I'm a complete noob at photon and I've been having a problem of correctly selecting the object that i shot to later use in an RPC, as it doesn't want to convert viewID into PhotonPlayer ID:

Thank you in advance!

[code2=csharp]void Update()
{
if(Input.GetMouseButtonDown(0))
{
if(GameObject.FindGameObjectWithTag(tagOfController).GetComponent<PlayerMatchSettings>().team == "predator") //if we're the predators
{
//DEBUG
Debug.Log("Clicked");

RaycastHit hit;


Vector3 fwd = transform.TransformDirection(Vector3.forward) * 20;

Vector3 forward = transform.TransformDirection(Vector3.forward) * 20;

Debug.DrawRay(transform.position, forward, Color.green);

if (Physics.Raycast(transform.position, fwd, out hit))
{

//DEBUG

Debug.Log("Hit");

Debug.Log(hit.rigidbody.gameObject.GetInstanceID());


//find the target id
int target = hit.rigidbody.gameObject.GetComponent<PhotonView>().viewID;
//find the corresponding photonplayer
PhotonPlayer target2 = PhotonPlayer.Find(target);
//send the rpc to the
Debug.Log(target);
Debug.Log(target2);

photonView.RPC("Shoot", target2);




}


}
}
}

//send info of being shot to client
[RPC]
void Shoot(PhotonMessageInfo info)
{
Debug.Log("Shot");

//don't check for yourself
if (!photonView.isMine)
{
//if i'm a hider
if(GameObject.FindGameObjectWithTag(tagOfController).GetComponent<PlayerMatchSettings>().team == "hider")
{
//it means that i'm marked and that i will die
PhotonNetwork.Destroy(photonView);
}
}
}

}[/code2]

Comments

  • your shoot RPC is missing the first parameter of type PhotonPlayer target basing on the parameters you send

    it should look like
    void Shoot(PhotonPlayer target, PhotonMessageInfo info)
    
  • Thank you very much! This has solved a bug that i haven't even met yet.

    The problem right now is the part where it has to find the correct PhotonPlayer, with info gained only from the physics.raycast.

    [code2=csharp]int target = hit.rigidbody.gameObject.GetComponent<PhotonView>().viewID;

    //find the corresponding photonplayer
    PhotonPlayer target2 = PhotonPlayer.Find(target);

    //send the rpc to the
    Debug.Log(target);
    Debug.Log(target2);

    photonView.RPC("Shoot", target2);[/code2]

    Thanks!
  • why do you use .viewId instead of simply using the owner which will be the one in question?

    on the other hand why do you send the shot on your photonview at all, why do you not call the shot on hit.rigidbody.gameObject.GetComponent<PhotonView>() directly so the 'being shot target' is informed that it was shot? The view you send it on is not even related to the target at the time.
    Generally whenever the target you want to inform of something has a view attached, you should probably consider to send the RPC directly to this object instead of sending it through your pipeline on behalf of that object as it might or might not be troublesome to look it up afterwards, what it surely is though is cpu wasting as you might have to lookup the view / explicit target object later on to have it updated properly.

    If your target was to be an authorative setup, then the one shooting would send a fire event and the server would decide if something was hit or not, you would not send the 'I want to fire' event and the target normally. Hence my question and a slight confusion. Perhaps you can explain what you want to achieve so the simplest path there could be recommended? :)
  • I also noticed how stupid i was with the code!

    For anybody else having the same nooby problems:

    [code2=csharp]//this code fetches the PhotonPlayer system and assigns it to the variable "target"
    PhotonView rootView = hit.rigidbody.gameObject.transform.root.GetComponent<PhotonView>();
    int targetID = rootView.owner.ID;
    PhotonPlayer target = PhotonPlayer.Find(targetID);[/code2]