- Home /
How to apply damage to raycast hitten object's Networkview?
Hello, I have a script on an empty gameobject wich includes the next variable: public static float Health;
In another script wich is attached to the player Object i have the next code:
hit.transform.gameObject.networkView.RPC ("SetHealth", RPCMode.AllBuffered, 10.0f, hit.transform.gameObject);
Debug.Log ("Hitted " + hit.transform.gameObject.name);
}
}
}
}
[RPC]
void SetHealth(float health, GameObject x)
{
if(x.networkView.isMine)
Player_stats.Life -= health;
}
Everything works, it tells me "Hitted player", but it doesn't take his health. It is so stressfull, please help me..
Answer by Robert Carlsson · Mar 17, 2014 at 11:52 PM
The Debug is on "your side" of the RPC call, try putting it inside SetHealth and see if you still get the message. If not the recieving computers never gets the call, do you have an established lan/internet connection?
Yes, the multiplayer works perfectly. The only thing is that i do not know how to get the Network View of the object i hit or how to send an execution command for SetHealth function to that specific player.
So the problem is with the if(x.networkView.is$$anonymous$$ine) then?
Try Setting a Index on each GameObject in a script.
Lets say Player 1 has index 0, set the index on the "Remote" version of him when you enable/instantiate it to 0. Ins$$anonymous$$d of passing a gameobject in the rpc call pass the Index. Then do:
void SetHealth(float health, int Index)
{
if(Index == myIndex)
Player_stats.Life -= health;
}
This way you wont have to keep track of the specific networkid but everytime a player joins the game, give him a Index. Then when you spawn his/her unit/character you set his Index in a script both on the local side and remote side.
Sorry for the late reply and I hope that helps!
Regards
Answer by Wolfrik_Creations · Jun 11, 2016 at 01:40 AM
Two years later and no one has told you that you cannot pass a GameObject through RPC.
If you read my latest post you see that I told him to use indexes ins$$anonymous$$d of the GameObjects.
Every client has a list/array of available characters where the characters are given their correct input/actions based on the index the server sends!
And every player gets his/her index assigned as soon as they join the server.
Didn't think I would have to be that specific since I thought he'd read the RPC manual on http://docs.unity3d.com/$$anonymous$$anual/net-RPCDetails.html. There it clearly states it can't be used:
" You can use the following variable types as parameters to RPCs:-
int
float
string
NetworkPlayer
NetworkViewID
Vector3
Quaternion
"
What I suggested is only one of many ways to handle the issue.
Best Regards