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

Question Newbie Simple 2D Animation Flip Help

Discussion in '2D' started by Nulubez, Mar 27, 2023.

  1. Nulubez

    Nulubez

    Joined:
    Feb 28, 2014
    Posts:
    1
    Hi everyone, I'm just starting out with Unity, and I'm having trouble with flipping a simple sprite animation based on the transform's rotation. Essentially, when I flip the weapon that the animation is attached to along the y axis, the animation itself is flipped along the z axis.

    The animation is simply adjusting the z rotation from 0 to -180 over thirty frames, with a pivot on the hilt of the sword, in order to create a basic overhead swipe motion. The two images below show the how the animation currently works in its beginning and end states.

    upload_2023-3-26_19-4-36.png upload_2023-3-26_19-5-42.png

    I currently have a very basic script that instantiates a prefab weapon and calls a flip function that checks whether the player is facing left or right. If the player is facing left, then weapon "transform.localScale = Vector3.Scale(-1,1,1)". The script then swings the weapon, and after the swing (based on animation time), the weapon is destroyed).

    However, when the player is facing left, while the weapon sprite is flipped along the y axis, the animation itself it flipped along the z axis. So instead of rotating the weapon's z axis by -180, it is rotated by 180. Meaning that while I want it to rotate like this:


    upload_2023-3-26_19-15-9.png

    What actually happens is it rotates like this:

    upload_2023-3-26_19-15-46.png

    This swings the sword behind and below the character, rather than in front of and above.

    I'm interested in figuring out what I'm doing wrong here. I'm assuming it has something do with Vector3.Scale, but I can't understand what's going on.
     
  2. Unrighteouss

    Unrighteouss

    Joined:
    Apr 24, 2018
    Posts:
    599
    You're overcomplicating this by a lot. If you make the sword a child of the player, the sword will inherit any changes made to the player's position, rotation and scale. You'll never have to worry about the sword's transforms, because it will all be taken care of for you automatically.

    Instantiating and destroying objects is a lot less efficient and more error prone than simply enabled/disabling objects. I would strongly advise enabling/disabling the weapon in animation instead of controlling that through code.