Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MaximilianPs · Sep 11, 2013 at 09:35 PM · c#animationsmelee

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 ! :(

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 :)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seth-Bergman · Sep 14, 2013 at 05:15 PM 0
Share

it may be possible through mecanim.. I still am not very familiar with mecanim

avatar image Jamora · Sep 15, 2013 at 03:50 PM 0
Share

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.

avatar image MaximilianPs · Sep 16, 2013 at 02:18 PM 0
Share

ehe.. it was just a matter of sorting the words on the correct way, here is an interesting tutorial too

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How can I call Animations per C# Script? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges