GetComponent for the client doesn't work properly in RPC calls or serverside calls?
So I've ended up getting into a pretty twisted and inefficient way of handling multiplayer shooting but it's kinda too late, I don't have enough time to redo it. I basically want to use a line renderer to draw a tracer for the raycasting shooting in our top down game.
The problem I have is that if the server completely handles the tracers then it looks off on the client since the client and server are out of sync. Instead I want to draw the tracer using client data for the client shooting and then use the server for everyone else.
Since the shooting is handled in the gunbase class which is attached to a gun (Which can't have a NetworkIdentity on it since the player already had one), I had to make a networked component called RenderTracer that handles the LineRenderer. Anyway, it was easy enough to make the clientside tracers but when I try to draw the server side tracers on all but the client, I've run into issues.
public class RenderTracer : NetworkBehaviour
{
[SerializeField]
private LineRenderer _tracer;
[SerializeField]
private Transform _firePoint;
private ClientManager _clientMgr;
private void Start()
{
_clientMgr = GameObject.Find("ClientManager").GetComponent<ClientManager>();
_tracer = _firePoint.GetComponent<LineRenderer>();
}
public void RenderNewTracer(Vector3 hitPoint, GameObject gunOwner)
{
if (isLocalPlayer && !isServer)
{
Debug.Log("Not server");
StartCoroutine("RenderTracerCoroutine", (hitPoint));
}
else
{
Debug.Log("Server");
StartCoroutine("RenderTracerCoroutine", (hitPoint));
Debug.Log("Gun owner: " + gunOwner.name);
foreach (uint player in _clientMgr._playerIDs)
{
GameObject playerGO = ClientScene.FindLocalObject(new NetworkInstanceId(player));
Debug.Log("Player: " + playerGO.name);
if (playerGO == gunOwner)
{
Debug.Log("Player shot: " + playerGO.name);
playerGO.GetComponent<RenderTracer>().RenderOtherTracer(hitPoint, gunOwner);
}
}
}
}
[ClientRpc]
void RpcRenderTracer(Vector3 hitPoint, GameObject gunOwner)
{
Debug.Log("RPC");
RenderOtherTracer(hitPoint, gunOwner);
}
void RenderOtherTracer(Vector3 hitPoint, GameObject gunOwner)
{
Debug.Log("RPC gameobject: " + this.gameObject.name);
if (!isLocalPlayer)
{
return;
}
Debug.Log("!Local player");
Debug.Log("Is " + this.gameObject.name + " owner of shot (" + gunOwner.name + "): " + (gunOwner == this.gameObject).ToString());
if (gunOwner != this.gameObject)
{
Debug.Log("Zoop");
StartCoroutine("RenderTracerCoroutine", (hitPoint));
}
}
IEnumerator RenderTracerCoroutine(Vector3 hitPoint)
{
Debug.Log("Tracer drawn");
_tracer.enabled = true;
_tracer.SetPosition(0, _firePoint.position + (_firePoint.forward * 2));
_tracer.SetPosition(1, _firePoint.position + hitPoint);
yield return null;
_tracer.enabled = false;
}
Don't judge code too bad (Though any improvements would be appreciated) but that's the code I have for it. The problem comes on this line:
playerGO.GetComponent<RenderTracer>().RenderOtherTracer(hitPoint, gunOwner);
The playerGO is correct and the line is only being called for that GameObject. Though even if the playerGO is not the server gameobject, the function RenderOtherTracer is still being called on the server. No matter what I do, I can't get the client component through server calls whether it's an RPC or direct like above. With RPCs if I call this.gameobject, it always gets the server's gameobject and not the clients one. Any ideas on why this doesn't work?
Example above is from the console of Player1 (Server). Why does Player1 see the Debug.Log("RPC gameobject: " + this.gameObject.name);
that should only be seen by Player2? On Player 2 (The client), the RPC gameobject will be the server even though it should be itself.