Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can't manage to properly flip my character

Discussion in '2D' started by Qothal, Mar 29, 2020.

  1. Qothal

    Qothal

    Joined:
    Mar 11, 2020
    Posts:
    5
    Hi, I need help..
    This may seem a lot to read, but I needed to give details :)

    I made a character using bones, made its animations etc and attached a script to it so i can move him around.
    I put the script on the main character object, which contains the root bone with the other bones and the body part sprites: https://imgur.com/SHqtYAd

    The flip function in my script uses transform.Rotate(0, 180, 0). This worked perfectly fine on my previous "project" where i used pixel art and sprite animation, rather than rigged characters, this is my first time.
    The problem with this is that it doesn't flips it correctly..

    I have a wall check game object attached to my character and it's a raycast. When my character flips, the raycast attached to it doesn't flip this results in my character being able to slide only on walls if he's facing right.

    Here's a picture that shows the axis being correctly oriented on my character, red for the X and green for the Y: https://imgur.com/iIVsEDA
    Another pic that shows the axis in the opposite direction, the Y axis apppears to be on the X, and the X appears to be on the Y, I don't know why.. https://imgur.com/gldV5Ue

    I figured that when i flip the character, the bones don't flip and I think that's why the raycast doesn't face the right direction of my character, I might be wrong idk..

    Please any suggestion, enlightening is appreciated, I can't find anywhere about this problem..
    I also tried flipping the root bone in the script using a tag, the character flips better, but the raycast still doesn't follow the correct facing direction.
    Photo of the flip function and where I did the raycast https://imgur.com/s04zTMR

    If I need to provide the whole code, please let me know.
     
  2. Qothal

    Qothal

    Joined:
    Mar 11, 2020
    Posts:
    5
    bump

    I fixed the wall check, but the character still doesn't physically flip.
    I even logged what angles are currently on my root bone (because this is what i'm trying to rotate), and the console displays them correctly, but in game my character doesn't flip - it's like the transform.rotate doesn't work?
    Anyone?
     
  3. Aratow

    Aratow

    Joined:
    Nov 4, 2016
    Posts:
    49
    You might want to try using Quaternion.Euler to flip the sprite.
    In my case tho, when i flipped the sprite it was invisible, so had to turn off the culling in the shader.
     
  4. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
  5. Xiromtz

    Xiromtz

    Joined:
    Feb 1, 2015
    Posts:
    65
    Your description confuses me a bit. What exactly do you WANT to flip? Is it the sprite, is it the collider, or is it the raycast? Is it every single object within the gameobject?
    In my opinion, I believe your approach in regards to this is wrong. To flip your sprite or bones, you want to create an animation that does it for you, instead of changing the transform manually.
    To flip your raycast, why not only rotate the gameobject containing your raycast?
    It also seems you're using a boxcollider, so why would you even bother flipping that if its the same regardless?

    Additionally, for 2D, you want to negate the scale of the axis (y-axis in your case i think) to do the flip instead of rotating. Have you tried that?
     
  6. Xiromtz

    Xiromtz

    Joined:
    Feb 1, 2015
    Posts:
    65
    Oh, and although I have barely any experience using bone rigging in 2d as opposed to sprite sheet animation, I believe bones act on their own plane of transform. I.e. you would need to get the component containing your rig and access the bone rotation and rotate that manually. Its probably better for you if you simply trigger an animation through code and do everything in the animator.
     
  7. Qothal

    Qothal

    Joined:
    Mar 11, 2020
    Posts:
    5
    I will consider trying this, thank you.

    Yes, I want to rotate everything, my character to act as one "thing".
    However, I fixed the problem using the scale, thanks for your reply!

    And the problem with the raycast i found a solution, I just changed where the raycast was pointing depending where the character is facing, it did the job, i don't know how practical it is but it works.
     
  8. Xiromtz

    Xiromtz

    Joined:
    Feb 1, 2015
    Posts:
    65
    @Kothal Good to hear it's working now. You might consider using an extremely thin box collider 2d instead of a raycast. This way it should definetely rotate with your gameobject and it stays permanent, so you don't have to raycast every frame or so.