Search Unity

Resolved Start animation with no delay

Discussion in 'Scripting' started by scythwolf, May 13, 2022.

  1. scythwolf

    scythwolf

    Joined:
    Dec 30, 2018
    Posts:
    49
    Hello!

    I encountered an issue that I am not sure how to solve or how to search for it.

    Right now I have set up an object with a sprite called 'Destructive Obstacle'. It has 2 children: anim, holding the animation, and Trigger_PlayerClose holding the BoxColliderr 2D for the trigger (see the picture).

    When the player gets close, the trigger... well... triggers and the animation is played. Initially I used this code:
    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         initialSprite.a = 0f;
    4.         parent.GetComponent<SpriteRenderer>().color = initialSprite;
    5.         animcontrollerDestuctiveObstacle.SetBool("playerEntered", true);
    6. }
    But... using this leads to the alpha of the sprite set to zero (so fine so good) but then the animation doesn't start immediately. There is a break of about 10 frames where nothing is visible.

    I thought about incorporating fixedupdate or update into this.
    Code (CSharp):
    1.     private void FixedUpdate()
    2.     {
    3.         if(playAnimation == true)
    4.         {
    5.             animcontrollerDestuctiveObstacle.SetBool("playerEntered", true);
    6.         }
    7.  
    8.     }
    9.     private void OnTriggerEnter2D(Collider2D collision)
    10.     {
    11.         initialSprite.a = 0f;
    12.         parent.GetComponent<SpriteRenderer>().color = initialSprite;
    13.         playAnimation = true;
    14.     }
    ... but as thought this does not solve the problem. Since I can't see the reason for this behaviour I am kinda lost. For every hint I'd be grateful.

    Thanks,

    Andreas
     

    Attached Files:

    Last edited: May 13, 2022
  2. scythwolf

    scythwolf

    Joined:
    Dec 30, 2018
    Posts:
    49
    I had times set under Settings of the animation. Now I deacitvated exit time and everything works flawlessly. Of course you find that immediately after posting
     
    Kurt-Dekker likes this.