Client side hit detection - passing IDs of hit items to server

For client side (player) hit detection, how should the ID of enemies hit be passed along?

I want to:

  1. Detect a hit with a hitscan on the player client.
  2. Pass a Cmd to the server indicating which players/objects were hit
  3. Have the server execute TargetRPC on the affected clients.

I am not finding any easy answers how how to pass IDs to the server that it can then use to target the affected clients. Ideally it would be a small number like a (byte)netID.value through the Cmd function, and then have the server translate that into a ConnectionToClient object.

Anyone dealt with this, or have any ideas on this?

unless you don’t care about hacking something like Hit detection is not something you want to do on the client. You will end up with MAJOR hacking issues.

1 Like

Yep, I am an old school quake competitive player - fully aware of the risks of client side anything. My intention is making it a server option and using it sparingly. But regardless there are other uses for this outside of client side hit detection.

The same question applies to things like giving the other player something or sending a private message based on looking at them. I am not yet seeing any clean and easy ways to pass another player id to the server and translate that into a TargetRPC to that player.

It has to exist, it just isn’t obvious in any documentation or tutorials I have found anywhere yet.

Dont really understand your problem, you know about rpc’s and doesnt care about security?

Raycast and rpc the “id” of whatever is hit that has your script attached to it?

Which ID are you referring to though? I can get the netID of the player object by hitting its collider with a raycast, but that is not the same ID as the connectionID needed by the server to TargetRPC that player. What seems to be missing (that I haven’t found yet) is a way to look up the connectionID for the player that owns the player gameobjects netID.

[TargetRPC]
void TargetTriggerThisEventOnMyVictim(HowDoIGetThisConnectionIDFromNetID) {
  // do a thing
}

Hey emotitron,

Sorry for the non-answers. I haven’t had to do what you’ve described but I thought that maybe what I found could give you a start. Just exploring all that the server owns looks like this might be a way to translate a NetID to a NetworkConnection:

using System.Linq;

// ...

NetworkInstanceID netId;
NetworkConnection owningConnection = NetworkServer.connections.Where( connection => connection.clientOwnedObjects.Contains(netId) ).FirstOrDefault();

I haven’t tested this so I’m not sure if this will work, but I think it might be a step in the right direction. As for truncating the NetID to a byte, I’m not sure how safe that is unless you can guarantee the ids will be able to be fully expressed in a byte.

Cheers!

Not as elegant as a function built into unet (seems like it should be in the HLAPI), but I will definitely give that a shot, thanks! Will report back when I try to apply it.

Like I said, I haven’t had to do it myself. There may be a much cleaner way that I’ve missed. Just didn’t want you to be empty-handed.

Edit: Just went searching for one again. Found a much cleaner way – don’t know how I missed it the first time

NetworkConnection connnection = NetworkServer.objects[netId].connectionToClient;
1 Like

No way, I’ve been looking for that for two days now if that is the answer. Will definitely post back if it works.

If the object hit is a networked object then passing NetworkIdenty.id to the server should work (and then find the object on server using ID). Or if there is a NetworkBehaviour script on the hit object you could call a [Command].

However if the hit object is non-networked then neither of these options work.

This is pretty much exactly the correct answer, thanks!

Awesome! Glad I could help.