Send variables to another player.

So I have two characters that can attack each other.

function OnCollisionEnter(collision : Collision) 
{
	
		if (canattack)
		{
		
			

			for (var contact: ContactPoint in collision.contacts)
			{
				var impact = Instantiate(impactPrefab, contact.point, Quaternion.FromToRotation(Vector3.up,contact.normal));
				collision.gameObject.SendMessage ("Damage", damage,SendMessageOptions.DontRequireReceiver);
			}
			
		}
}

I also have a variable on each player, “isBlocking”. If the player gets hit while he is blocking then the player hitting should be given the “isStunned = true”.

So I did this:

 function gotBlocked(damage: float){

       isStunned = true;
       yield WaitForSeconds(damage/2);
       isStunned = false;

   
 }

 function Damage(damage : float)
{
if(!isBlocking)
	{
		health -= damage;
	}
	else
	{
	gotBlocked(damage/2);
	}


}

But all it does is to apply isStunned to the player that is getting hit. How can i solve this?

1 Answer

1

Because if the player that is blocking gets hit, the function Damage will be called on their object, they are blocking so the “GotBlocked” is going to get called, what i would do is this:

public class Player (){

	bool canAttack;
	public bool isBlocking;
	public bool isStunned;
	float attackPower = 5f;
	float health = 100f;

	void OnCollisionEnter (Collision col){
		if(canAttack){
			if(col.collider.GetComponent<Player> ()){
				Player otherPlayer = col.collider.GetComponent<Player>();
				if(otherPlayer.isBlocking){
					this.Stun ();
				} else {
					otherPlayer.Damage(attackPower);
				}
			}
		}
	}

	public void Damage (float damage){
		health -= damage;
	}

	public void Stun (float damage){
		isStunned = true;
		yield WaitForSeconds(damage/2);
		isStunned = false;
	}
}

sorry for the c# instead of java

That's incorrect. The current latest version of Unity still has this issue. It has not been fixed.