- Home /
Is it possible to block a collider with a trigger from hitting and object behind it?
Hi, Im working on a melee combat system which is currently setup this way:
The weapon has a RigidBody (set to kinematic and with gravity off) and a capsule collider (set as a trigger), when the weapon's collider hits the enemy it sends a message to apply damage. This is working great for me and it's most likely the way I will go about this since it's the most realistic and precise melee method I could find.
Now Im trying to develop a shield that will block these attacks but I can't seem to figure out the best way to do it. The idea is that if the weapon's collider hits the shield it wont do any damage to the character holding it, but the problem is that since this collider is a trigger it goes right trough the shield and hits the character anyways.
I realize I can probably use "Physics.IgnoreCollision" or simply disable the enemy's collider/rigidbody so that when he's holding the shield it doesnt do any damage, but this is not really what Im looking for, I want enemies to be able to block incoming attacks and still take damage if they are hit on the back (or anywhere other than the shield).
One possible solution Im thinking of is implementing a "bounce back" animation, so that if you hit a shield you'll immediately bounce back thus preventing the weapon from ever hitting the enemy, but Im not sure how to call an animation on a collision event or if that would even work at all.
Here's the code in charge of sending the damage notification:
var player : GameObject;
var AttackDamage : float = 25.0;
function Start ()
{
Physics.IgnoreCollision(collider, player.collider);
}
function OnTriggerEnter(other : Collider)
{
if(other.GetComponent(EnemyAI))
{
other.gameObject.SendMessageUpwards("ApplyDammage", AttackDamage, SendMessageOptions.DontRequireReceiver);
}
}
Answer by Spinnernicholas · Dec 03, 2013 at 01:06 AM
I would store a blocked flag on the sword that will stay true as long as it is colliding with a shield. Then, when it hits another part of the character, it will know that it is block and you can prevent it from sending a damage message.
bool blocked = false;
GameObject blockedBy;
//.........
void OnTriggerEnter(Collider other)
{
if(other.GetComponent(Shield)
{
blocked = true;
blockedBy = other.gameObject;
}
else if(other.GetComponent(EnemyAI) && !blocked)
{
other.sendMessage("ApplyDammage", AttackDamage, SendMessageOptions.DontRequireReceiver);
}
}
void OnTriggerExit(Collider other)
{
if(other.gameObject == blockedBy)
{
blocked = false;
blockedBy = null;
}
}
Awesome! Im going to have to tweak it a bit since it seems slightly unreliable but the basics for the implementation do seem to work so I think this will be the way to go.
Thank you kindly for your answer, it helped me out a great deal :)
I wrote it on the fly, without testing it, so it will probably need lots of tweaking. I'm glad I could help you in the right direction. just a note this is a re-purposing of a script to detect when I character in a platformer is grounded.
Your answer
Follow this Question
Related Questions
Objects placed manually collide, but don't if instantiated? 1 Answer
OnTriggerEnter not being called, have trigger, RB, and colliders set? 1 Answer
Trigger via Collider 1 Answer
Can't click gameobject when over another trigger? 1 Answer
Do continuous and continuous dynamic collision detection work with trigger colliders? 1 Answer