- Home /
How to make network player lose health after being hit by rigidbody?
Hi, im trying to solve this for three days by now, so i ask here:
I have a multiplayer game with working authoritative moving players. Whats also working well is network instantiating fireballs done by server on players request. When a fireball collides something, it succesfully Network.destroys itself by function OnCollisionEnter and spawns my explosion prefab.
But what i need it to do is that when the fireball hits "something" tagged as Player, he has to lose 1 health. I have tried around a hundred ways to make this happen, but not a single on was working.
Can anybody help me with this problem?
This is my working code attached to fireball that destroys the fireball itself:
function OnCollisionEnter(collision : Collision) {
// Rotate the object so that the y-axis faces along the normal of the surface
var contact : ContactPoint = collision.contacts[0];
rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
pos = contact.point;
if(Network.isServer){
Network.Instantiate(explosionPrefab, pos, rot, 0);
Network.Destroy (gameObject);
}
}
I have tried, for example to put into this code
if(collision.gameObject.tag == "Player"){
collision.gameObject.SendMessage("ApplyDamage");
}
but it worked in one from five shots, or to put into Playerscript the OnCollisionEnter and if the tag is "Fireballshot", gave me pretty much the same result.
I also tried to try a trigger attached on the fireball and function OnTriggerEnter in the Playerscript and many other ways and im just desperate.
HELP!
Answer by jirdano · Apr 06, 2012 at 09:54 PM
ok, so i moved to this:
i have separate healthscript on player:
var maxHealth:int=5; static var curHealth:int=5;
function Awake(){ // We are probably not the server -> only server can control this script if(Network.isClient){ enabled=false; // disable this script
}else if(Network.isServer){ enabled=true; // enable this script } }
function OnCollisionEnter(collision:Collision){ if(collision.gameObject.tag == "FireballShot"){ if(Network.isServer){ collision.gameObject.SendMessage("DestroyYourself"); curHealth -=1; if(curHealth<1){ //Network.Destroy(gameObject); transform.position = GameObject.Find("SpawnRedTeam").transform.position; transform.rotation = GameObject.Find("SpawnRedTeam").transform.rotation; curHealth=maxHealth; } } } } would work well, only problem is that when one player hits another, the the health is lost on both players and i dunno why
hey its just me again spam$$anonymous$$g my own thred :) i figured it on my own again, it was just about changing static variable to public, hope it helps somebody
Your answer