Only host can damage players.
So my problem here is that my script health that manages my health and damage is not working properly. Upon entering the game the host can kill all clients. But the clients can not damage each other and the clients can not damage the host. After some searching on google i've come to the conclusion that something is wrong in this part of the script:
function Damage(damage : float)
{
if (isServer)
{
RpcDamage(damage);
}
else
{
CmdDamage(damage);
}
}
@Command
function CmdDamage(damage : float)
{
Damage(damage);
}
@ClientRpc
function RpcDamage(damage : float)
{
health -= damage;
}
}
Keep in mind it's written in UnityScript / JavaScript. I'm getting no errors regarding it.
I have tried even more and I cannot get it figured out. It seems to be just very very odd..
I still have not found any answer to this problem... Does nobody know how to solve it?
I don't know much about networking, but I'm in the middle of my own little online project, so I'm looking for a solution to your problem. I imagine that RpcDamage isn't called on the server, so to fix that problem, you'd just write:
@Command
function CmdDamage(damage : float)
{
health -= damage;
Damage(damage);
}
But of course I'm not sure.
Why don't you use a SyncVar to track the health?
@Command
function CmdDamage(damage : float)
{
health -= damage;
}
@SyncVar
var health : float;
I do use syncvar. It's just not being synced properly. The health variable does have the syncvar tag yet it's not properly being damaged.
What I've written is all that's nececssary to make it work...
function Damage(damage : float)
{
if (!isServer)
{
return;
}
health -= damage;
}
I have a seperate script on the same gameobject (the player prefab). called Weapon. so the weapon script has a OnCollisionEnter and is being called as follows:
collision.gameObject.Send$$anonymous$$essage ("Damage", damage,Send$$anonymous$$essageOptions.DontRequireReceiver);
If you want the full script I can send that. So that is how it's being sent over to the health script wich is on the same object and has the exact code you wrote above:
function Damage(damage : float)
{
if (!isServer)
{
return;
}
health -= damage;
}
Can you put a Debug.Log line in the Damage function to see if it's actually executing?
It sounds like you're probably trying to run a Command on the client, which is why it only works one way around.
I can't help with actual code as I don't know UnityScript, but the basic process needs to work like this:-
Get a reference to the GameObject you hit.
Then use a Command function to call a 'takedamage' function on the hit GameObject (the client can't call the function directly, which is why it only works one way on your tests).
The 'takedamage' function should be marked with the [server] attribute, so it only runs on the server copy of the script, and it should be in a script that also contains the health syncvar. It can then modify the health value which will then propagate its new value to all other clients and run any code contained in the hook function.
$$anonymous$$y UNET tutorial has a simple example of how to go about this http://www.doofah.com/tutorials/networking/health-and-damage/
The most common reason for the kind of problem you are encountering is due to clients trying to directly execute code that needs to be executed on the server.
How would you code it in C#? Could you provide me with an example so I can understand it easier?
See my comment above, I provided a link to my UNET tutorial which provides about as simple an example as you can get of how to handle it in C#.
To answer your comment below, it depends on how you have coded your attack function, but in my tutorial I use a raycast to get a reference to the gameobject, however you could just as easily use collisions.
This would probably be better dealt with in the community forum than here.
Also, how do I make a reference to the gameobject I hit? Any C# Examples?
Your answer
Follow this Question
Related Questions
RPC Not working 0 Answers
Question to RPC and Command (Unet) 0 Answers
ClientRpc/Commands between two seperate projects? 0 Answers
Damage Players Using Raycast 0 Answers
How to update player from another player in Local Player Authority 0 Answers