Hello, If someone could point me in the right direction or a post on this, I would be grateful! I am creating a 2D top down shooter from a tutorial on Udemy. I want to add some enhancements and one of them is animating the space ship as it banks left or right. The ship will always face North and South and pivot on the z Axis. I have a 5 frame animation for both directions, left and right. I cannot figure out how to apply this using an animation controller or blend tree. I was thinking of manually changing the sprite based on a counter but it seems the correct way of doing this is with animation. If more information is needed, please let me know! Thanks in advance for any help!
You can have the animator play animations based on a âparameterâ that you define in the animator graph, which is set via code and the animator will react to those changes.
Say you add a parameter âTiltâ. Maybe thatâs an integer that will be -1 for tilt left, and 1 for tilt right. It could be a float for more precision, but it seems you just have two new animations to add based on moving left or right.
You will have a âIdleâ or âDefaultâ state for when the ship is moving normally, and then you will make new states (drag an animation clip directly to the animator graph, or create the state and drag the clip into the inspector) that play your animations for left and right tilting, which shouldnât have an exit time (because you want the ship to stay tilted).
The transition between those states will have conditions using the parameter, like âtilt equal to 1â or âtilt equal to -1â and connected to the right and left states respectively. You will also need transitions back to the idle state with âTilt equal to 0â.
Then in code, when you press the left key, you will use a reference to your Animator component to call âmyAnimator.SetInteger(âTiltâ, -1)â. Then when you let go of that key, and the right key is not being pressed, you call âmyAnimator.SetInteger(âTiltâ, 0)â.
The transitions and states in the animator should be able to go between each other using those values to trigger a transition. You donât want transition time on the states because youâre not blending animations, you just want to immediately play the next state.
Hope that helps.
More detail and examples:
Thank you! I appreciate you in helping my by posting some information. I have tried part of your suggestion but will follow it more closely when I am available to test it. Right now I am at work. This is the problems I remember:
The transition from tilt (either left or right) back to idle, it will need to repeat the animation backward or mirrored and then start the animation for the opposite tilt. I will definitelytry this again and update with my troubles. thank you again!
I havenât tried specifically, but I believe you can duplicate your tilt animation state, and set the speed to -1 in the inspector, and transition back to default through that (using exit time of 100% so the whole thing plays before transitioning to the default state).
You can always create the tilting back animation separately if you need it to be different in some way.
So youâll have something like this with an integer parameter âTiltâ:
Your tilting clips shouldnât be set to loop or anything either.
An issue I find with using Unityâs animator and sprite animations is that you wonât be able to say, interrupt the right transition halfway and go back to center animation starting from that frame without some weird hacks.
Iâve coded my own simple sprite animator in the past to handle this kind of simple sprite animation stuff on a frame-number basis.
Thereâs also this free asset Iâve used in the past called Animation2D: https://assetstore.unity.com/packages/tools/animation/animation2d-sprite-sheet-animation-95812
Oh Wow! Thank you jefferyschoch for the help you have given me!
âAn issue I find with using Unityâs animator and sprite animations is that you wonât be able to say, interrupt the right transition halfway and go back to center animation starting from that frame without some weird hacks.â
This was a problem i was having and why I was thinking I had to manually change the sprites. I will focus on the info you have given me and I really, really appreciate the screen shot and the time you spent to do that for me! I will return with my result. You have definitely pointed me in a direction as I stood in the middle of a forked road!
You solution worked! However, there is a what seems to be a âhesitationâ in responsiveness. I am sure it may be something with my code handling the turn events, but knowing it works thrills me. I an going to investigate the link you given me for the free asset. I downloaded and look at the sample and it seems interesting. If I fail at getting the result I want, my next decision will be to turn it into a 3D top down shooter that behaves like a 2D game. I have lots of experience modeling and I know it will be way more easy to rotate the ship on the z axis. plus I think it will give it a cool look. But since I was doing the tutorial on Udemy, I thought I would go out on a limb. Thanks!