Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Is control functions by animations and per frame with script the best choice?

Discussion in 'Scripting' started by meierdesigns_unity, Apr 26, 2020.

  1. meierdesigns_unity

    meierdesigns_unity

    Joined:
    Apr 16, 2020
    Posts:
    7
    Hey guys,
    I am working on a beginners Diablo tutorial, that works pretty well, exept the animation and damage controlls.

    The tutorial I chose is very well explained as far as I'm concerned but because of it's age I think unity would call it depricated. Here is the link:


    I come from an artist background and want to enter the field of development. Therefor I am willing to research the different methods of animation and time control to achieve a precise damage timing.

    I have asked different developers on how to control functions by the current frame of an animation. As the tutorial is from long time ago. 2014 to be exact... I think meanwhile there are better and preciser methods to deal with the problem. To describe it I want the following:

    I have two skelettons. The first is controlled by me as the player. The second is the mob and I want to attack.
    The animation of the attack is 24 frames long. the hit happens at frame 16.

    upload_2020-4-26_10-52-15.png

    I want to know what ways there are to control the damage function by frame and what methods you prefer. What are your pros and cons?

    I have tried a way that converted the full length of the animation and the current time the animationis it and converted it to a number between 0 and 1. Before I tried to calculate a condition when the current animation has reached the 0.8 * the length of the full animation clip. I find this method very vague and I want to control the damage exactly by frame and not a calculated number that has a long decimal number. I have not found a good video that compares the different methods on how to control the damage timing but maybe you do - and I would be greatfull for any help on this.

    For my discribed example - here is my code for the hit and hit reset function:

    The first function (impact) is supposed to calculate the time the animation lasts and when the damage is applied to the mob. Also there is a bool (impacted) to set on true when the damage is dealt. After the damage is dealt the function is not supposed to be called until (impacted) is false again.

    The second function is the hit reset function which calculates when the (impacted) bool is set back to false. It calculates that the hit reset is happening after the animation is over completly.


    Code (CSharp):
    1.  
    2.     public void impact()
    3.     {
    4.         // animationTime = GetComponent<Animation>()[attack.name].time / GetComponent<Animation>()[attack.name].length;
    5.  
    6.         if (animationTime == 1) { Debug.Log("Animation Time within impact is 1 - " + animationTime); }
    7.  
    8.         // Debug.Log("impactLengthTime: " + impactLengthTime); Debug.Log("impactTime: " + impactTime); Debug.Log("impactLength: " + impactLength);
    9.         // if (opponent != null && GetComponent<Animation>().IsPlaying("attack") && !impacted && GetComponent<Animation>()[attack.name].time > GetComponent<Animation>()[attack.name].length * 0.9)
    10.         if (!impacted)
    11.         {
    12.             if (opponent != null && animationTime > impactHitTime)
    13.             {
    14.                 opponent.GetComponent<Mob>().getHit(damage);
    15.                 // Debug.Log("hit at " + animationTime);
    16.                 impacted = true;
    17.                 countDown = combatEscapeTime;
    18.                 CancelInvoke("combatEscapeCountDown");
    19.                 InvokeRepeating("combatEscapeCountDown", 0, 1);
    20.                 // if (GetComponent<Animation>()[attack.name].time < GetComponent<Animation>()[attack.name].length) { }
    21.             }
    22.         }
    23.     }
    24.  
    25.  
    26.  
    27.     void resetHit()
    28.     {
    29.         // animationTime = (float)(GetComponent<Animation>()[attack.name].time / GetComponent<Animation>()[attack.name].length);
    30.         animationTime = aniattack.time / aniattack.length;
    31.  
    32.         // Debug.Log("resetHit exists and animationTime is: " + animationTime);
    33.  
    34.         // if (animationTime >= 0.95 && animationTime <= 0.999) { Debug.Log("Animation Time 1 - " + animationTime); }
    35.         // if (animationTime >= 0.5 && animationTime <= 0.55) { Debug.Log("Animation Time 0.5 - " + animationTime); }
    36.         // if (animationTime >= 0.3 && animationTime <= 0.35) { Debug.Log("Animation Time 0.3 - " + animationTime); }
    37.         // if (animationTime >= 0.2 && animationTime <= 0.25) { Debug.Log("Animation Time 0.2 - " + animationTime); }
    38.  
    39.         if (animationTime >= 0.95 && animationTime <= 0.999)
    40.         {
    41.             Debug.Log("Reset time is: " + animationTime);
    42.             PlayerMovementDiablo.attack = false;
    43.             // impactTest = false;
    44.             Debug.Log("hit reset");
    45.             impacted = false;
    46.         }
    47.     }
    I don't think this is very good to handle and I want to be able to aply it to other situations and to call up particles and sounds when a certain frame has past.

    So should I continue to fix the problem that comes up using my method or what alternatives are there to use precise animation per frame control?


    Thank you very much for the time you took to go through all of this!

    Best regards
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    look at animation triggers for frame specific events

    upload_2020-4-27_2-27-35.png
     
  3. meierdesigns_unity

    meierdesigns_unity

    Joined:
    Apr 16, 2020
    Posts:
    7
    Well that was exactly the way I solved the problem. Though I had to redo all the animation of the characters. Thanks for mentioning this way. But are there any other ways to get to a good character or animation control?