Search Unity

Question Procedural control of animation. What are ways to do that?

Discussion in 'Getting Started' started by darkzarichv2, Jul 20, 2022.

  1. darkzarichv2

    darkzarichv2

    Joined:
    May 4, 2022
    Posts:
    3
    Hello!

    So, basically let's say we have a player that can perform a special attack: jump, hit the ground pushing everything away with some particle effects

    With my knowledge this is what i got:

    Code (CSharp):
    1.     private IEnumerator Animate(Player player)
    2.     {
    3.         var playerRb = player.GetComponent<Rigidbody>();
    4.  
    5.         // Jump animation
    6.  
    7.         playerRb.AddForce(Vector3.up * 15, ForceMode.Impulse);
    8.  
    9.         yield return new WaitForSeconds(0.5f);
    10.  
    11.         playerRb.AddForce(Vector3.down * 50, ForceMode.Impulse);
    12.  
    13.         yield return new WaitForSeconds(0.15f);
    14.  
    15.         playerRb.velocity = Vector3.zero;
    16.  
    17.         // Push enemies back
    18.  
    19.         IEnumerable<Enemy> enemies = SpawnManager.EnemyList.Where(e => Vector3.Distance(e.transform.position, player.transform.position) <= 10f);
    20.  
    21.         foreach (Enemy enemy in enemies)
    22.         {
    23.             Vector3 directionAway = (enemy.transform.position - player.transform.position).normalized;
    24.  
    25.             enemy.GetComponent<Rigidbody>().AddForce(directionAway * _knockbackStrength, ForceMode.Impulse);
    26.         }
    27.  
    28.         Instantiate(_fxPrefab, player.transform.position, Quaternion.identity);
    29.     }
    But it seems very verbose and really hard to control, it also feels like using
    WaitForSeconds 
    is wrong because I have to nail down the timings and the timings may differ depending on the situation.

    What are other ways to achieve what I want? How does one usually handle all that sequence of actions stuff with precise control over what part to execute next?

    Thanks!
     
  2. darkzarichv2

    darkzarichv2

    Joined:
    May 4, 2022
    Posts:
    3
    Was able to almost get rid of timings but I don't know...

    Code (CSharp):
    1.     private IEnumerator Animate(Player player)
    2.     {
    3.         var playerRb = player.GetComponent<Rigidbody>();
    4.  
    5.         float jumpTime = Time.time + _hangTime;
    6.         float startYPoint = player.transform.position.y;
    7.  
    8.         while (Time.time < jumpTime)
    9.         {
    10.             playerRb.velocity = new Vector2(playerRb.velocity.x, _smashSpeed);
    11.  
    12.             yield return null;
    13.         }
    14.  
    15.         while (player.transform.position.y > startYPoint)
    16.         {
    17.             playerRb.velocity = new Vector2(playerRb.velocity.x, -_smashSpeed);
    18.  
    19.             yield return null;
    20.         }
    21.  
    22.         playerRb.velocity = Vector3.zero;
    23.  
    24.         // Push enemies back
    25.  
    26.         foreach (Enemy enemy in SpawnManager.EnemyList)
    27.         {
    28.             enemy.GetComponent<Rigidbody>().AddExplosionForce(_knockbackStrength, player.transform.position, _radius, 0.0f, ForceMode.Impulse);
    29.         }
    30.  
    31.         Instantiate(_fxPrefab, player.transform.position, Quaternion.identity);
    32.  
    33.         StopCoroutine(Animate(player));
    34.     }