- Home /
Melee and Animation: sync attacker and victim
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 ! :(
Answer by Seth-Bergman · Sep 12, 2013 at 08:26 PM
I believe you should be able to sync the hit animation to the other exactly using AnimationState.normalizedTime... something like:
if(attacker.animation["Attack"].normalizedTime >= .5){
victim.animation[beHit.name].normalizedTime = (attacker.animation["Attack"].normalizedTime - .5) * 2;
}
(where "victim" and "attacker" are the gameObjects..) this should do it I think.. So your code would look like:
IEnumerator BeHit(float attackSpeed) {
animation.Stop();
animation[beHit.name].wrapMode = WrapMode.Once;
gettingHit = true;
}
void Update(){
if(gettingHit){
//VERSION 1
//use this to also sync speed of anims.. (so they sync exactly)
if(attacker.animation["Attack"].normalizedTime >= .5){
animation[beHit.name].normalizedTime = (attacker.animation["Attack"].normalizedTime - .5) * 2;
}
//VERSION 2
//or just this should do it for your needs I think (plays at exactly halfway through attack):
if(attacker.animation["Attack"].normalizedTime >= .5){
animation.Play(beHit.name);
//for Play(), we only want to call for one frame... so we reset gettingHit
gettingHit = false;
}
}
//this is to reset after the anim plays... (necessary for first version only)
if(gettingHit){
if(!animation.IsPlaying(beHit.name){
gettingHit = false;
}
}
}
the top version would sync the anim frame by frame, whereas the second just Plays the anim at the right moment.. just delete the other version you don't use.. untested, but this should give you the idea...
Answer by MaximilianPs · Sep 14, 2013 at 05:12 PM
I thought you could insert a function into a specific frame of the animation maybe via Animator Window... but maybe I've dreamed it ^_^
Thank you anyway, I've fixed it by adding a bool and adding a .normalizedTime check :)
it may be possible through mecanim.. I still am not very familiar with mecanim
It is possible to add animation events from the animation window. The button is right next to the add keyframe button. A quick (google) search on "Unity Animation Events" should give a load of info.
ehe.. it was just a matter of sorting the words on the correct way, here is an interesting tutorial too
Answer by TheHobbyist · Oct 15, 2018 at 01:25 AM
As mentioned in this comments here, I have found Animation Events to be the superior solution for Attacker/Target timing synchronization. Far better than hard coding times and percentages all over the place.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnTriggerEnter triggering twice on one collider? 5 Answers
animation.speed not working 0 Answers