Stopping something damaging local player
For the game I am developing we have 'Area of effect' abilities that a player can spawn a particle effect, anyone in this effect will get damaged however it is not supposed to damage the player who created it but it does.
I've tried mutliple added a bool to the Witch scrip that all players have that spawn these abilities -
Just to note both solutions stop the player damaging themselves when only they are in a room, but when someone else joins it no longer works
I've tried mutliple added a bool to the Witch scrip that all players have that spawn these abilities -
void OnParticleCollision(GameObject col){ if(col.gameObject.tag == "Player") { if(col.transform.GetComponent<WitchScript>() != null){ if(!col.transform.GetComponent<WitchScript>().localPlayer) { col.collider.SendMessage("ApplyDamage", AstralDamage, SendMessageOptions.DontRequireReceiver); } } else { col.collider.SendMessage("ApplyDamage", AstralDamage, SendMessageOptions.DontRequireReceiver); } } }Tried setting a variable in the script with the creators photonView ID and then referencing back to it
void OnParticleCollision(GameObject col){ if(col.gameObject.tag == "Player") { if(col.transform.GetComponent<WitchScript>() != null){ if(col.transform.GetComponent<PhotonView>().viewID.ToString() != parent) { col.collider.SendMessage("ApplyDamage", AstralDamage, SendMessageOptions.DontRequireReceiver); } } else { col.collider.SendMessage("ApplyDamage", AstralDamage, SendMessageOptions.DontRequireReceiver); } } }
Just to note both solutions stop the player damaging themselves when only they are in a room, but when someone else joins it no longer works
0
Comments
-
Both colliders have PhotonViews on them?
You can check if one of the colliders (the one that would take damage) is yours. Use: PhotonView.isMine.0 -
Sorry about the badly formatted post, fixed the formatting now.
I already had .isMine in my damage script
Here i my damage script.void ApplyDamage(int Damage) { if(photonView.isMine) { hp -= (int)Damage; tpc._attackingState = AttackingState.TakeDamage; animController.SetLayerWeight(1, 1); animController.SetInteger("AttackingState", (int)tpc._attackingState); StartCoroutine(attackAnimations()); } if (hp <= 0) { theScoreBoard.LocalPlayerHasKilled(); photonView.RPC("Respawn", PhotonTargets.All); } else { photonView.RPC("setHP", PhotonTargets.Others, (int)hp); } } [RPC] void setHP(int newHP) { hp = newHP; } [RPC] void Respawn() { if(photonView.isMine){ theScoreBoard.LocalPlayerDied(); GameObject spawnpoint = GameObject.FindGameObjectWithTag("SisterhoodSpawn"); gameObject.transform.position = spawnpoint.gameObject.transform.position; hp = maxHp; mana = maxMana; } }
Before I made the first post the line;hp -= (int)Damage;
was out side of the photonView.isMine, I moved it inside and it works better but i still seem to take damage every so often any ideas why this may be?
Also here is the option I am using for the sending of messages not sure if this is right or notvoid OnParticleCollision(GameObject col){ if(col.gameObject.tag == "Player") { if(col.transform.GetComponent<WitchScript>() != null){ if(col.transform.GetComponent<PhotonView>().viewID.ToString() != parent) { col.collider.SendMessage("ApplyDamage", AstralDamage, SendMessageOptions.DontRequireReceiver); } } else { col.collider.SendMessage("ApplyDamage", AstralDamage, SendMessageOptions.DontRequireReceiver); } } }
EDIT: Just realized I didn't answer your previous question yes both colliders have photon views on them, The Player can instantiate the particle effect then when another player enters the particle effect it damages them, well that's how it's supposed to work0 -
If a player is initiating the particle system, then I suggest you let that player control the damage report of the particles locally, and then send the damage to the clients of who's being affected. In the Unity reference, it says:This message [OnParticleCollision] is sent to all scripts attached to the WorldParticleCollider and to the Collider that was hit.
So, I suggest doing something like this (I coded on the fly, and I'm much faster at coding in JS, so apologies);//attach to particle system //reference photonview var photonView : PhotonView; //this should be our parent that has the collider on it var parentCollider : GameObject; function Start(){ //reference our photon view photonView = gameObject.GetComponent(PhotonView); } function OnParticleCollision(object : GameObject){ //check if it's us thats controlling it. remember, we want to 'monitor' it locally first if(photonView.isMine){ //now ignore if particle has hit us - as we dont want to send ourselves damge //&& that its a player if(object.transform != parentCollider && object.gameObject.tag == "Player"){ //okay, now we need to get the photonViews of the objects we've hit var targetID = object.gameObject.GetComponent(PhotonView); //now that we have the targets photonView, we can send a remote message - damage targetID.photonView.RPC("ApplyDamage", PhotonTargets.All, AstralDamage); } } } //now we need to make the RPC for the ApplyDamage, //attach this anywhere on the player - on the parent might be better @RPC function ApplyDamage(damage : int){ hp -= damage; //note - we don't need to check if 'photon.isMine', as this is //already controlled by the original sender. //i guess better to be safe than sorry though :-/ }
0 -
When instantiating the Particle Effect, send a parameter with it which contains the PhotonNetwork.player.ID variable. Then when a player collides with the particles, only apply damage if OwnerID (call this however you want) != the colliders PhotonNetwork.player.ID
This should work and is easily done.0