Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Beginner needs help with "SpriteAnimator"

Discussion in 'Unity Connect' started by RidarTV, Oct 2, 2020.

  1. RidarTV

    RidarTV

    Joined:
    Sep 27, 2020
    Posts:
    3
    Hello Unity Community, I need help with my SpriteAnimator, it should play 5 different Animation, but it doesnt, it only play IdleFront and walkFront, can someone help me please? :)

    here is the code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using CodeMonkey;
    5. using CodeMonkey.Utils;
    6.  
    7. public class GameHandler : MonoBehaviour
    8. {
    9.     [SerializeField] private SpriteAnimator spriteAnimator;
    10.     [SerializeField] private Sprite[] idleAnimationFrameArrayFront;
    11.     [SerializeField] private Sprite[] walkingAnimationFrameArrayFront;
    12.     [SerializeField] private Sprite[] walkingAnimationFrameArrayBack;
    13.     [SerializeField] private Sprite[] walkingAnimationFrameArrayLeft;
    14.     [SerializeField] private Sprite[] walkingAnimationFrameArrayRight;
    15.  
    16.  
    17.     private enum AnimationType
    18.     {
    19.         IdleFront,
    20.         WalkFront,
    21.         WalkBack,
    22.         WalkRight,
    23.         WalkLeft,
    24.     }
    25.     private AnimationType activeAnimationType;
    26.  
    27.     private void Start()
    28.     {
    29.         PlayAnimation(AnimationType.IdleFront);
    30.     }
    31.  
    32.     private void Update()
    33.     {
    34.         bool isMovingBack = false;
    35.         bool isMovingFront = false;
    36.         bool isMovingLeft = false;
    37.         bool isMovingRight = false;
    38.         if (Input.GetKey(KeyCode.S))
    39.         {
    40.             isMovingFront = true;
    41.         }
    42.         if(isMovingFront)
    43.         {
    44.             PlayAnimation(AnimationType.WalkFront);
    45.         }
    46.         else
    47.         {
    48.             PlayAnimation(AnimationType.IdleFront);
    49.         }
    50.  
    51.         if (Input.GetKey(KeyCode.W))
    52.         {
    53.             isMovingBack = true;
    54.         }
    55.         if (isMovingBack)
    56.         {
    57.             PlayAnimation(AnimationType.WalkBack);
    58.         }
    59.         if (Input.GetKey(KeyCode.D))
    60.         {
    61.             isMovingRight = true;
    62.         }
    63.         if (isMovingRight)
    64.         {
    65.             PlayAnimation(AnimationType.WalkRight);
    66.         }
    67.         if (Input.GetKey(KeyCode.A))
    68.         {
    69.             isMovingLeft = true;
    70.         }
    71.         if (isMovingLeft)
    72.         {
    73.             PlayAnimation(AnimationType.WalkLeft);
    74.         }
    75.  
    76.     }
    77.  
    78.     private void PlayAnimation(AnimationType animationType)
    79.     {
    80.         if (animationType != activeAnimationType)
    81.         {
    82.             activeAnimationType = animationType;
    83.             switch (animationType)
    84.             {
    85.                 case AnimationType.IdleFront:
    86.                     spriteAnimator.PlayAnimation(idleAnimationFrameArrayFront, .2f);
    87.                     break;
    88.                 case AnimationType.WalkFront:
    89.                     spriteAnimator.PlayAnimation(walkingAnimationFrameArrayFront, .1f);
    90.                     break;
    91.                 case AnimationType.WalkBack:
    92.                     spriteAnimator.PlayAnimation(walkingAnimationFrameArrayBack, .1f);
    93.                     break;
    94.                 case AnimationType.WalkRight:
    95.                     spriteAnimator.PlayAnimation(walkingAnimationFrameArrayRight, .1f);
    96.                     break;
    97.                 case AnimationType.WalkLeft:
    98.                     spriteAnimator.PlayAnimation(walkingAnimationFrameArrayLeft, .1f);
    99.                     break;
    100.             }
    101.  
    102.         }
    103.     }
    104. }
    105.  
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class SpriteAnimator : MonoBehaviour
    7. {
    8.     public event EventHandler OnAnimationLoopedFirstTime;
    9.     public event EventHandler OnAnimationLooped;
    10.  
    11.  
    12.     [SerializeField] private Sprite[] frameArray;
    13.     private int currentFrame;
    14.     private float timer;
    15.     private float framerate = .2f;
    16.     private SpriteRenderer spriteRenderer;
    17.     private bool loop = true;
    18.     private bool isPlaying = true;
    19.     private int loopCounter = 0;
    20.  
    21.     private void Awake()
    22.     {
    23.         spriteRenderer = gameObject.GetComponent<SpriteRenderer>();
    24.     }
    25.    
    26.    
    27.     private void Update()
    28.     {
    29.         if(!isPlaying)
    30.         {
    31.             return;
    32.         }
    33.         timer += Time.deltaTime;
    34.  
    35.         if(timer >= framerate)
    36.         {
    37.             timer -= framerate;
    38.             currentFrame = (currentFrame + 1) % frameArray.Length;
    39.             if (!loop && currentFrame == 0)
    40.             {
    41.                 StopPlaying();
    42.             }
    43.             else
    44.             {
    45.                 spriteRenderer.sprite = frameArray[currentFrame];
    46.             }
    47.  
    48.             if(currentFrame == 0)
    49.             {
    50.                 loopCounter++;
    51.                 if(loopCounter ==1)
    52.                 {
    53.                     if (OnAnimationLoopedFirstTime != null) OnAnimationLoopedFirstTime(this, EventArgs.Empty);
    54.                 }
    55.                  
    56.                
    57.                 if (OnAnimationLooped != null) OnAnimationLooped(this, EventArgs.Empty);
    58.             }
    59.            
    60.         }
    61.     }
    62.     private void StopPlaying()
    63.     {
    64.         isPlaying = false;
    65.     }
    66.  
    67.     public void PlayAnimation(Sprite[] frameArray, float framerate)
    68.     {
    69.         this.frameArray = frameArray;
    70.         this.framerate = framerate;
    71.         currentFrame = 0;
    72.         timer = 0f;
    73.         spriteRenderer.sprite = frameArray[currentFrame];
    74.     }
    75.  
    76.  
    77.  
    78. }
    79.