Home > Software engineering >  How to make multiplayer players not damage themselves?
How to make multiplayer players not damage themselves?

Time:09-13

So, I'm using Photon Pun 2 to make a basic multiplayer combat game. When the game starts, two player prefabs are instantiated, one for each player. Each prefab's hand has a "PlayerHand" tag and when the enemy's hand hits the player, the player should take damage, like this:

public class GetCollision : MonoBehaviour
{
    public PlayerController me;
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("PlayerHand"))
        {
            Debug.Log("hit"   Time.deltaTime);
            me.TakeDamage(20);
        }      
    }
}

public class PlayerController : MonoBehaviourPunCallbacks
{
    public void TakeDamage(float damage)
    {
        PV.RPC(nameof(RPC_TakeDamage), PV.Owner, damage);
    }

    [PunRPC]
    void RPC_TakeDamage(float damage, PhotonMessageInfo info)
    {
        currentHealth -= damage;
    }
}

The problem I'm having is, as each player's own hand has a "PlayerHand" tag, the players can do damage to themselves. Is there a way to make player take damage only if the "PlayerHand" is not it's own? Something like:

if (PlayerHand != isMe) { takeDamage() }

Or is there some other way to achieve this?

All advice is appreciated!

CodePudding user response:

maybe you could make it so every time you hit, you gain the amount of health you lose when you get hit. so if 2 players have 20 health and when you get hit you lose 10 health then make yourself gain 10 health whenever you try to hit

CodePudding user response:

You could try to create two separate scripts, one is for the players health, the other is for the hand that can apply the damage.

For example lets call the the scripts PlayersHealth and PlayersAttack.

On the PlayersHealth script you should have the following method (This script should be added on to the player):

    public class PlayersHealth : MonoBehaviourPunCallbacks
    {
      [PunRPC]
      public void TakeDamage(float amount)
      {                
        health -= amount;
      }
    }

On the PlayersAttack script you should check to see if the opponent has the PlayersHealth script and call the method during a trigger event (This script should be added on to the player's hand)

    public class PlayersAttack : MonoBehaviourPunCallbacks
    {
        public PlayersHealth playersHealth;
        public float damage;

        void Start()
        {
          if(playersHealth == null)
          {
            return;
          }
        }

        private void OnTriggerEnter(Collider other) 
        {
            if(other.CompareTag("PlayerHand"))
            {
                playersHealth = other.transform.GetComponent<PlayersHealth>();
                if (playersHealth != null)
                {
                   if (photonView.IsMine)
                   {
                      //This should ignore the owning player.                
                   }
                   else
                   {
                     playersHealth.photonView.RPC(nameof(playersHealth.TakeDamage), RpcTarget.All, damage); 
                   }                  
                }
            }
        }
    }

Note: This is a modified script from one of my project which I adjusted to try to guide you to solve your problem, it has not been tested.

  • Related