I think it would be awesome to see an example of someone setting up the block to work with colliders. What I mean by that is that the shield just shields the enemies sword from the attack. So if the player blocks late, or facing the wrong enemy they still take damage when the shield wouldn’t actually prevent damage.
The easiest way to manage both of these is to check to see if the player’s Targeter is targeting the same character that is attacking. This is because in TargetingState we are always facing our target, so naturally, it would be the facing the right way to block and the only character whose damage is blocked.
Colliders are a bit trickier. You could put a collider on the shield and activate it when entering blocking state and deactivating it when leaving blocking state. Then just checking to see in the collision if other == the shield. (I think I would probably put a dummy “Blocker” behaviour on the shield
if(other.TryGetComponent(out Blocker blocker)
{
return;
}
As in the lecture the other suggestion was to set up a “cone”, which in which the attacker has to be blocked. I implemented the ability to block outside of target lock so Brians solution wouldn’t work for me.
in health, create a weapon damage method which gets called from weaponDamage, is passed the enemies transform and then finds whether the enemy is within a particular angle of the players forward.
note that the otherTransform passed from OnTriggerEnter in the weaponDamage script will be the transform.root.transform (or CharacterCollider.transform). Otherwise the direction will be to enemies hand transform which will often give you odd results.
private const float blockCoverageAngle = 15f;
public bool TakeWeaponDamage(float damage, Transform otherTransform)
{
if (isBlocking && AttackComingFromFront(otherTransform)) { return false; }
else
{
TakeDamage(damage);
OnKnockBack?.Invoke();
return true;
}
}
private bool AttackComingFromFront(Transform otherTransform)
{
Vector3 playerDirection = otherTransform.position - transform.position;
playerDirection.y = 0f;
float angle = Quaternion.Angle(
Quaternion.LookRotation(transform.forward),
Quaternion.LookRotation(playerDirection));
return angle <= blockCoverageAngle;
}
Whilst collider/hitbox is more realistic again, I think the added complexity isn’t necessarily worth it. Sometimes added realism actually makes gameplay feel odd. e.g. most hitboxes are considerably larger than the items they are attached to or else the gameplay can feel pretty clumsy.
If you still want to head down that path though, its pretty much the same as the weapon. A script on the shield/left hand with methods to turn its collider off and on again, which could be handled by either animation events or just entering and exiting the state as the animation we are using isn’t that dynamic.
In the ontriggerenter method from the enemies sword, check if the collider is attached to the player and has the shield script attached to it. If it does, skip the damage method.
You would probably have find the players character collider and add it to the alreadyHit list so that any follow through wouldn’t catch the player and deal damage anyway, or find some way to IK it off the shield (I know pretty much nothing about IK at the moment so can’t help you there)
IK chains could be a course in and of themselves. I find the time involved in setting them up is seldom worth the reward.
Same… I put it directly in PlayerStateMachine. I then added a way to set if the character is stunned from Impact and check against that. Also to make sure the player isn’t dead.
This has the added benefit of canceling attacks when you block. Which is my desired behavior. I will do the same for dodging.
But this is why I’d like to try to conical block approach. It SEEMS easy enough but you hit the nail on the head with the making the colliders much bigger than the object. I am already having so much trouble landing attacks with the cool animations I found. Gotta make enemy hitboxes larger and/or weapon hitboxes larger. Ironically, I watched a GDC talk today about good combat and the devs of Dreamscaper talked about having several overlapping colliders for things like projectiles. A nice thick one to be better at hitting enemies only, with a smaller one inside that collides with walls and stuff so you have more leeway to land a hit without it getting caught in geometry. So it doesn’t sound like this is bad behavior or anything.
I really hope to tweak this system all the way. I plan to combine it with what I have made in the RPG course for a really fun experience.