I need help coding a shield system.

Hi, Im trying to figure out a way to implement a shield for a project Im working on, the idea is that if you hold the shield button the character will raise it and stop any incoming attacks.

For starters, here’s the code Im using to send the damage notification:

var hit : RaycastHit;
	if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit)) 
	{
		Distance = hit.distance; 
		if (Distance < AttackRange) // Checks if the raycasted object is in the range of attack
		{
			yield WaitForSeconds(0.5);
			// Calls the function "Apply Damage" on the object that's being hit
			hit.transform.SendMessage("ApplyDammage", attackDammage, SendMessageOptions.DontRequireReceiver);
		}
	}

Basically the enemy will make a Raycast and if the player is within the attack range it’ll send a notification to the player to reduce his damage.

So, Im thinking that the idea here is that if the player is holding his shield then he wont be able to receive the “ApplyDammage” message or he will ignore it, but of course the question is how do I do that.

Also, I would like it so that only the shield itself stops the attack, so if the player is holding the shield up but gets hit on the back he still receives damage.

Any ideas on how I can do this? Im thinking maybe I can add a collision mesh to the shield and if the enemy hits that then it wont do any damage, but Im not sure how to make that sort of thing work.

If you want a shield you could try something in this fasion…

#pragma strict
var shielded : boolean = false;

function Update() {
if(Input.GetMouse(1)) {
shielded = true;
}else{
shielded = false;
}

if(shielded == true) {
//disable playe damage or something here
}
}