Problem with RPC take damage

Options
Hey everyone Im trying to cause damage to one player.

One player attacks, the other one recieves the RPC and executes the method. The problem is: For example if the player one attacks the player 2 executes the method but the damage it's decremented in player 1 and vice-versa.

What am I doing wrong
void AttackShout()
	{

		if(photonView.isMine)
		{
			photonView.RPC("IWasAttacked", PhotonTargets.Others, PhotonNetwork.player.ID);
		}
	}


	private void TakeDamage()
	{
		health = health - 10;
		Debug.Log("Im with " + health + " health");

		if(health == 0)
		{
			Debug.Log("Crap ... Im dead.");

			this.enabled = false;
		}
	}

	[RPC]
	private void IWasAttacked(int playerID)
	{
		string log = "I was Attacked by " + playerID;
		Debug.Log(log);
		TakeDamage();
	}

Comments

  • vadim
    Options
    Hi,

    You call IWasAttacked method on all clients except attacked.
    I may want call RPC for PhotonTargets.All or call TakeDamage() for attacked player directly.
  • captalvez
    Options
    I changed my code to make the damage be handled from a class GameCombat that handles all the logic from the combat.

    So I have a gameObject with a photon view that recieves the player that was attacked and then informs the other players. My problem is how do I access the player attacked by is player ID to affect his health? I feel that Im close but I cant find a way to do this.

    Heres my player class
    using UnityEngine;
    using System.Collections;
    
    public class Player : Photon.MonoBehaviour {
    
    	public float speed = 10f;
    
    	private float lastSynchronizationTime = 0f;
    	private float syncDelay = 0f;
    	private float syncTime = 0f;
    	private Vector3 syncStartPosition = Vector3.zero;
    	private Vector3 syncEndPosition = Vector3.zero;
    
    	public static int health;
    
    	private RaycastHit hit;
    	private float dist;
    
    	void Start()
    	{
    		health = 100;
    	}
    
    	// Update is called once per frame
    	void Update () 
    	{
    		if(photonView.isMine)
    		{
    			InputMovement();
    			InputColorChange();
    			Attack();
    		}
    		else
    			SyncedMovement();
    			
    	}
    
    	void InputMovement()
    	{
    		if (Input.GetKey(KeyCode.W))
    			rigidbody.MovePosition(rigidbody.position + Vector3.forward * speed * Time.deltaTime);
    		
    		if (Input.GetKey(KeyCode.S))
    			rigidbody.MovePosition(rigidbody.position - Vector3.forward * speed * Time.deltaTime);
    		
    		if (Input.GetKey(KeyCode.D))
    			rigidbody.MovePosition(rigidbody.position + Vector3.right * speed * Time.deltaTime);
    		
    		if (Input.GetKey(KeyCode.A))
    			rigidbody.MovePosition(rigidbody.position - Vector3.right * speed * Time.deltaTime);
    	}
    
    	void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    	{
    		...
    	}
    
    	private void SyncedMovement()
    	{
                   ...
    	}
    
    	private void Attack()
    	{
    		if(Input.GetKeyDown(KeyCode.K))
    		{
    			GameCompat.Attack(getPlayerAttackedID());
    		}	
    	}
    
    	private int getPlayerAttackedID()
    	{
    		RaycastHit hit;
    		
    		if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit))
    		{
    			if(hit.distance < 3)
    			{
    
    				return hit.transform.gameObject.GetPhotonView().ownerId;
    			}
    
    		}
    
    		return 0;
    	}
    
    	public void TakeDamage()
    	{
    		health = health - 10;
    		Debug.Log(health);
    	}
    
    }
    

    Heres the GameCombat class that handles the combat logic - recieves which player was attacked and should affect his health accordingly
    using UnityEngine;
    using System.Collections;
    
    public class GameCompat : MonoBehaviour {
    
    	private static PhotonView scenePhotonView;
    
    	void Start () 
    	{
    		scenePhotonView = this.GetComponent<PhotonView>();
    	}
    	
    
    	void Update () {
    
    	}
    
    	public static void Attack(int playerID)
    	{
    		scenePhotonView.RPC("AttackedPlayer", PhotonTargets.All, playerID);
    	}
    
    	[RPC]
    	public void AttackedPlayer(int playerID)
    	{
    		if(playerID == 0)
    			Debug.Log("No one was hit");
    		else
    		{
    			Debug.Log("Player " + playerID + " was hit.");
                            //Affect the health of the player attacked HERE ---------------------- ?
    		}
    	}
    	
    }
    
  • Tobias
    Options
    You can access who sent an RPC by adding an (optional) parameter of type PhotonMessageInfo as explained here:
    http://doc-api.exitgames.com/en/pun/cur ... #rpcManual

    The .sender should help you.
    If the sender is not the interesting info, then you need to add the PhotonPlayer.ID to the parameters of your RPC.