I NEED TO FIND A WAY TO SYNC 2 ANIMATIONS:
The aggressor’s animation which HIT the victim, and the victim which become HIT by the aggressor, because the “beHitted” is played with a wrong timing respect the attacker.
I use the ray-casting to check if they are at the right distance and at the correct angle to hit the target, booth AI and Player have a common class called BaseCharacter which have a function DoAttack()
- there is a timer called attackTimer, every time that a character attack the timer will be set to “2” (seconds) and then will be subtracted everyframe, at 0 you attack again.
`
public void DoAttack(float weaponRange, int damage, string AttackAnimation)
{
RaycastHit hit; // What we hit ?
GameObject raycaster = GetChild(“MeleeRaycaster”); //method to find the empty object which cast rays
if (target == null) // run this routine to assign the target
{
if(Physics.Raycast(raycaster.transform.position,transform.TransformDirection
(Vector3.forwar) ,out hit))
{
if (hit.transform.GetComponent<BaseCharacter>() == null)
return;
if (hit.transform.GetComponent<BaseCharacter>().GetVital(0).CurValue > 0f)
target = hit.transform.gameObject;
}
}
// distance from me and the enemy
float distance = Vector3.Distance(target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position).normalized; // calculate the angle from me and my target
float direction = Vector3.Dot(dir, transform.forward);
//if the distance and the corner are correct attack
if (distance <= weaponRange && direction > 0.6f)
ApplyDamage(target, damage);
else
Debug.LogWarning("SWISHHHH !!!");
//at the end play the animation..
SendMessage(AttackAnimation, GetSkill((int)SkillName.Attack_Speed).AdjustedBaseValue);
}
`
so… if we hit the target we call the ApplyDamage function which pass the target and the damage, also, this method will call an animation which is “BeHit” … and here I’ve the problem…
public void ApplyDamage(GameObject go, int damage)
{
attackTimer = 2; // this will reset the attack time I've been hitted for the next 2 sec I can't attack
go.GetComponent<BaseCharacter>().GetVital(0).CurValue -= damage;
go.SendMessage("BeHit", GetSkill((int)SkillName.Attack_Speed).AdjustedBaseValue);
AdjustHealth();
}
the problem is that I have no idea on how to sync. the animations Attack and beHit
actually i’m using this function
`
IEnumerator BeHit(float attackSpeed)
{
float sum = 1.5f / attackSpeed;
yield return new WaitForSeconds(sum);
animation.Stop();
animation[beHit.name].wrapMode = WrapMode.Once;
animation[beHit.name].speed = 1f;
animation.Play(beHit.name);
}
`
Here I get half time of the attack speed which is the time needed to play the StrongAttack animation (plus or minst in the middile of the animation is the point the the victim where be hitted) … but it’s hardcoded and also, it’s absolutely appropriative so I really need some suggestions 'cause I’m lost !