- Home /
How to handle raycast shooting in multiplayer (Mirror)?
EDIT: Code below ended up working fine. Still, any critique of my code is very much appreciated because I am still a bit confused about my code.
I'm not new to Unity anymore, but new to networking. Trying to make a multiplayer fps prototype. I definitely do not understand something, watching tutorials doesn't really help. PROBLEM: On host (server + client) side raycast shooting works perfect but client is unable to register a hit. Please help!!
void Update()
{
if(Input.GetKeyDown(KeyCode.Mouse0) && isLocalPlayer)
{
if(isServer)
Shoot(netId);
else
CmdShoot(netId);
}
}
[Server]
public void Shoot(uint owner)
{
RaycastHit hit;
// My guess is it doesn't work because playerCamera is different on different players (?)
if(Physics.Raycast(playerCamera.position, playerCamera.forward, out hit, weaponRange))
{
if(hit.collider.CompareTag("Player"))
{
PlayerMovement hitPlayer = hit.collider.GetComponent<PlayerMovement>();
if(hitPlayer.netId == owner)
return;
if(hitPlayer.isServer)
hitPlayer.ChangeHealthValue(-1);
else
hitPlayer.CmdChangeHealthValue(-1);
}
else
{
Debug.Log("No player was hurt");
}
}
}
[Command]
public void CmdShoot(uint owner)
{
Shoot(owner);
}
Ok something strange happened, now it works fine. I changed some lines without testing before submitting a question (changes are in the code above) and it seems to be working but the thing is, I don't know why it didn't work before (I don't remember what I changed). So, I guess, the question is not actual anymore, sorry for that. BUT if you see that something is done very wrong in my code, please, tell me. Thanks in advance!
Your answer
Follow this Question
Related Questions
Lag Compensated Projectile 0 Answers
Multiplayer Game Highlight Material 0 Answers