Friday, November 11, 2022
HomeGame Developmentunity - Customized Idle Animation not working

unity – Customized Idle Animation not working


I am attempting to insert the idle animation within the if-else assertion however every time I run the sport, it would not play the idle animation, simply the working animation. The bottom of this algorithm is from the Youtube tutorial of the Pokemon recreation. Right here is the hyperlink to that : Customized Animation

That is the code for the Character Animation

var prevAnim = currentAnim;

if (MoveX == 1)
{
    currentAnim = runRightanim;
}
else if (MoveX == -1)
{
    currentAnim = runLeftanim;
}
if (MoveY == 1)
{
    currentAnim = runUpanim;
}
else if (MoveY == -1)
{
    currentAnim = runDownanim;
}
if(currentAnim != prevAnim || isMoving != waspreviouslymoving)
{
    currentAnim.Begin();
}


if (isMoving)
{
    currentAnim.HandleUpdate();
}
else 
{
    if (MoveX == 1)
    {
        currentAnim = idleRightanim;
      
    }
    if (MoveX == -1)
    {
        currentAnim = idleLeftanim;
    }
    if (MoveY == 1)
    {
        currentAnim = idleUpanim;
    }
    if (MoveY == -1)
    {
        currentAnim = idleDownanim;
    }
}

waspreviouslymoving = isMoving;

That is the code for the Sprite renderer

 SpriteRenderer spriteRenderer;
Listing<Sprite> frames;
float frameRate;
int currentframe; //tracks body
float timer; //tracks time
public SpriteAnimator(Listing<Sprite> frames, SpriteRenderer spriteRenderer, float frameRate = 0.16f)
{
 //Initializer
    this.frames = frames;
    this.spriteRenderer = spriteRenderer;
    this.frameRate = frameRate;
}
public void Begin()
{
    currentframe = 0;
    timer = 0f;
    spriteRenderer.sprite = frames[0];
}

public void HandleUpdate()
{
    timer += Time.deltaTime;
    if (timer > frameRate)
    {
        currentframe = (currentframe + 1) % frames.Rely; //present body==final body, loops frames
        spriteRenderer.sprite = frames[currentframe];
        timer -= frameRate; //reset timer
    }
}

Im new to recreation Growth in Unity, I hope in your spare time, thanks~

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments