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

My character seems to flip on certain areas

Discussion in '2D' started by janjicm, May 21, 2020.

  1. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    Video showing the problem:


    Code for moving:
    Code (CSharp):
    1. movementInputDirection = Input.GetAxisRaw("Horizontal");
    Code for flipping:
    Code (CSharp):
    1. private void Flip()
    2.     {
    3.         isFacingRight = !isFacingRight;
    4.         transform.Rotate(0f, 180f, 0f);
    5.     }
    He rotates normally. When I press to go left he will rotate for 180 and when I press to go right he will rotate to 0. When I'm moving and this happens with a head he doesn't change his rotation.
     
  2. BJWelling

    BJWelling

    Joined:
    May 21, 2020
    Posts:
    2
    Hi janjicm,

    More code is needed to find the problem. For example the code around where you call the Flip function.

    The movie shows a character walking to the right. During this movement it seems to be vibrating the whole time, as if constantly flipping, is this desired?
     
  3. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    No it's not, in unity it doesn't look that bad but when you build it it's very noticeable. That vibrating is problem I guess, it looks like it's flipping

    Here is code where I call Flip
    Code (CSharp):
    1.  private void CheckMovementDirection()
    2.     {
    3.  
    4.         if(isFacingRight && movementInputDirection < 0)
    5.         {
    6.             Flip();
    7.         }
    8.         else if(!isFacingRight && movementInputDirection > 0)
    9.         {
    10.             Flip();
    11.         }
     
  4. BJWelling

    BJWelling

    Joined:
    May 21, 2020
    Posts:
    2
    More code is needed. But maybe you can try:

    Code (CSharp):
    1.  
    2. if(isFacingRight && movementInputDirection < -0.01f)
    3. {
    4.     Flip();
    5. }
    6. else if(!isFacingRight && movementInputDirection > 0.01f)
    7. {
    8.     Flip();
    9. }
     
  5. janjicm

    janjicm

    Joined:
    Apr 29, 2020
    Posts:
    16
    still same, I mean I can give you my whole code but you are wasting your time
     
  6. brigas

    brigas

    Joined:
    Oct 4, 2014
    Posts:
    522
    try to change transform rotate to

    Code (CSharp):
    1. transform.localScale *= new vector3(-1,0,0);