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

Question Making my second character face to the left at scene start

Discussion in '2D' started by OriSofer, Dec 21, 2022.

  1. OriSofer

    OriSofer

    Joined:
    Jul 25, 2021
    Posts:
    2
    Hello,
    This might be really basic, and I might be missing something that's just under my nose.
    So, I have a two player 2d game where I want both characters to start the round with faces directed at each other (1st player facing right, 2nd facing left).

    In my sprite-sheet (this is a frame animation via animator component) I've made both players facing to the RIGHT.

    Right now both of them start facing to the right when the scene is loaded, and I need the 2nd player to be facing to the left.

    this is my current super basic Flip() func:

    Code (CSharp):
    1. private void Flip()
    2.     {
    3.         if (rb.velocity.x < 0 && isFacingRight)
    4.         {
    5.             isFacingRight = !isFacingRight;
    6.             transform.Rotate(0, -180, 0);
    7.         }
    8.  
    9.         if (rb.velocity.x > 0 && !isFacingRight)
    10.         {
    11.             isFacingRight = !isFacingRight;
    12.             transform.Rotate(0, -180, 0);
    13.         }
    14.     }
    where isFacingRight is a private bool.

    I've tried like 4-5 different approaches. And This seems like a very basic problem. I really want to avoid flipping all my sprites to face to the left and then re-make all the animations.

    Am I missing something really basic?
    Thanks in Advance
     
    Last edited: Dec 21, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,963
    Simplify the above code:

    - use the velocity being less or greater than zero to blindly set the isFacingRight flag

    - use isFacingRight to either set the rotation to (0,0,0) or (0,180,0)

    - set the isFacingRight appropriately in the editor for each character (or at spawn time)

    That's it.

    EDIT: you may wish to ask if velocity is less than -0.1f or greater than +0.1f rather than zero
     
  3. OriSofer

    OriSofer

    Joined:
    Jul 25, 2021
    Posts:
    2
    Hi,
    Thank You much for taking the time and helping me. This is solved!
     
    Kurt-Dekker likes this.