- Home /
How to do a frontal-only shield of sorts?
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!
Answer by equalsequals · May 24, 2010 at 05:15 PM
You could try raycasting and if it intersects the collider they cause no damage.
[Edit]
If the raycast method doesn't work for whatever reason, you could try using some sort of comparison of the 2 melee-fighters Transform.forward. Perhaps a Dot Product (Vector3.Dot) between the 2 , if it is less than 0 I believe then they would be facing each other. Combine that with an arbitrary thresh hold and that could work for detecting whether an angle of approach should be blocked or not.
Don't quote me on this code, as it is just a quick scratch to get you started:
function OnCollisionEnter(other:Collision):void{ var otherV3:Vector3 = other.collider.gameObject.transform.position;
if(Vector3.Dot(transform.position,otherV3) < 0) { print("facing"); }}
Hope that helps.
==
Wouldn't I run in to the same problem with raycasting though? No matter how I set up the box collider, if the enemy gets too close he clips through it (even though he shouldnt be able to), so the "melee projectiles" hit my player anyway
I suppose that could be a case. I'll revise my answer with another idea I have.
Your answer
Follow this Question
Related Questions
Network - Projectile, Prevent Collision with Parent 2 Answers
Projectiles without colliders or rigid bodies? 1 Answer
Special Collider? 1 Answer
object with multiple colliders ignoring mousedown for some. 3 Answers
Ignore Collider 0 Answers