Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to tilt 2D Top down spaceship when moving left and right. (Animation w/ Sprites)

Discussion in '2D' started by GarrettDR, Apr 23, 2019.

  1. GarrettDR

    GarrettDR

    Joined:
    Feb 24, 2019
    Posts:
    14
    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!
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    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:
    https://docs.unity3d.com/Manual/AnimationParameters.html
     
    Last edited: Apr 23, 2019
  3. GarrettDR

    GarrettDR

    Joined:
    Feb 24, 2019
    Posts:
    14
    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!
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    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":
    animator.png

    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
     
    Last edited: Apr 24, 2019
  5. GarrettDR

    GarrettDR

    Joined:
    Feb 24, 2019
    Posts:
    14
    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!
     
    LiterallyJeff likes this.
  6. GarrettDR

    GarrettDR

    Joined:
    Feb 24, 2019
    Posts:
    14
    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!
     
    LiterallyJeff likes this.