I'm trying to figure out the best way to implement a shield for a 3rd person melee combat game. Basically what I want to do is make it so that when I hold down the right mouse button, all attacks that hit my player in the front do no damage. My melee system is very simple, the characters right now are just shooting projectiles that disappear after a few feet.
I tried having a box collider parented to my player, positioned at the front, and activate/deactivating it on Fire2, but if I get too close to my enemies, their attacks actually go through the box collider because they're clipping through it.
Is there a way I could calculate the position of the collision of the melee projectiles when they hit my character, and determine if they are on the front of him?
UPDATE [SOLVED]:
Awesome! Thanks to equalsequals,I got it figured out with the Vector3.Dot. The vector math is baffling to me, but by constantly printing the dot product of the melee projectile's forward, and the forward of the player (the object the collision is happening on), I figured out that if the dot product is -1, the projectile and the player are facing each other. Here's the jist of the final code:
function OnCollisionEnter(other : Collision){
var forward = transform.TransformDirection(Vector3.forward);
var otherForward = other.transform.TransformDirection(Vector3.forward);
var dotProduct = Vector3.Dot(forward,otherForward);
if(dotProduct < -0.9){
print("in the face!");
}
else{
print("not in the face!");
}
}
I went with the range of -0.9 - -1.0 to give it some leeway. Thanks again Equalsequals!