Mobile Games In Unity: Flip Sprite and Pause Walk Animation

In this article I’ll quickly go over how to set up the sprite to flip and face the correct direction as well as how to get the sprite to pause for a few seconds once they reach the waypoint.

So right now I’ll get a handle to the animator and the Sprite Renderer. I need a handle to the animator to be able to transition from the walk animation and the idle animation. I need a handle to the Sprite Renderer so I can flip it! With this simple line of code, my sprite will now flip depending on the current target.

So now when I run the game I get the result Im looking for.

If you’re following along with me, you may have noticed that your Moss Giant is moving even when the idle animation is playing. Theres a couple of things I need to do to make the transition happen and have the animation reflect that. First I need to set up the transitions in the Animator!

I set up a trigger parameter to handle the transition from walk to idle. There are some other things to take into consideration here though. I’m going to be fighting this thing. I dont want it to continue moving when im fighting it. So I only want it to transition to the walk animation when its not in combat with me. So I’ve also set up a bool to handle this.

So when the Moss Giant is not in combat it’ll transition to the walk animation. I want this to have exit time so that it plays the idle animation all the way through.

Next I need to set the transition from walk back to idle. Im just going to take advantage of the IdleTrigger parameter I created. I know I have “has exit time” checked here, but I DO NOT want exit time.

So now that the Animator is set up. Lets hop into the code for this. The goal is to have the walk animation only happen when the moss giant is moving. When I start the game the idle animation plays. Then the walk animation. In the update function I want to introduce the “GetCurrentAnimatorStateInfo”.

Learn more about this here:

Unity — Scripting API: Animator.GetCurrentAnimatorStateInfo (unity3d.com)

Im saying here, if the idle animation is playing and the inCombat parameter is false OR if the moss giant is dead, dont do anything, otherwise start moving.

As you can see I’ve made a few bools. I’ve also got a handle to the player game object too.

I want to have the giant stop when I hit it and attack the player back, so isHit is the bool to handle that. Right now, my goal is to simply have the giant move back and forth and stop at each waypoint.

So here Im saying if isHit is false (which it is for now) start moving to the currentTarget. I also want to control the inCombat parameter judging by the distance the moss giant is away from the player. So if distance is greater than 2 units, inCombat is false. Using this code, if you jump back intoyour game and run it, the moss giant should be moving from point a to point b and back, stopping at each way point to cycle through an idle animation. :)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store