Networking: Handling object interactions

So far I have coded a simple synced online ‘game’ with authoritative server. I’m trying to implement interaction(shooting mainly) between player avatars and simple NPC but have been stuck for a day, so thought this is the right place for help!

The problem: I’m trying to implement a simple targeting of players and NPCs where the target information gets transfered to the server via RPC and if player attacks, health is reduced from the right instantiated gameobject server-side.

It’s working very well client-side: I cast a Ray from the camera and from the hit I get GameObject which I then set as a target locally. The problem is sending the target information to the server as an RPC. I initially sent the target as “NetworkPlayer” and it seemed to work decently, but NPC-s don’t have NetworkPlayers.

What is the usual practice of handling such types of interactions?

Should I send networkview ID as the target and let the server figure out correct gameobject?

Ideally, I’m after something like this:

  1. Client selects target.
  2. Target information sent to server as RPC
  3. Client presses hotkey to fire a gun
  4. Firing a gun information sent to RPC
  5. Server does security checks and applies damage
  6. New health is synced accross the network

I figured it out myself. I just send the Network View ID and then on the server side find the appropriate object:

// Client side
public void SetTarget(GameObject target)
{
    this.target = target;
    viewID = target.networkView.viewID;

    networkView.RPC("ServerSetTarget", RPCMode.Server, viewID);
}

// Server side
    [RPC]
    public void ServerSetTarget(NetworkViewID viewID)
    {
        if(Network.isServer)
        {
           GameObject target = NetworkView.Find(viewID).gameObject;
        }
    }