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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

trying to make this sprite face in the direction that it last faced

Discussion in '2D' started by weareallthereis, Nov 28, 2019.

  1. weareallthereis

    weareallthereis

    Joined:
    May 17, 2015
    Posts:
    8
    im trying to make this sprite face in the direction that it last faced after using the joystick:
    Code (CSharp):
    1. if (Mathf.Abs(angle) > 90)
    2.             {
    3.                     facingRight = false;
    4.                     Vector3 scale = transform.localScale;
    5.                     scale.x = Mathf.Abs(scale.x);
    6.                     transform.localScale = scale;
    7.             }
    8.             else if (Mathf.Abs(angle) < 90)
    9.             {
    10.                     facingRight = true;
    11.                     Vector3 scale = transform.localScale;
    12.                     scale.x = -1;
    13.                     transform.localScale = scale;
    14.              
    15.             }
    16.                 if (facingRight)
    17.                 {
    18.                     Vector3 scale = transform.localScale;
    19.                     scale.x = -1;
    20.                     transform.localScale = scale;
    21.                 }
    22.                 else if (!facingRight)
    23.                 {
    24.                     Vector3 scale = transform.localScale;
    25.                     scale.x = Mathf.Abs(scale.x);
    26.                     transform.localScale = scale;
    27.                 }
    All it does is snap back to facing the default direction when I stop using the joystick.

    variable angle doesnt print any value when joystick isn't being used (doesnt say angle is 0, possible it's null)
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @weareallthereis

    What are you actually trying to accomplish?

    I wouldn't like to start guessing, but if you are trying to typical 4-directional movement, you don't have change your sprite unless there is some input.
     
  3. weareallthereis

    weareallthereis

    Joined:
    May 17, 2015
    Posts:
    8
    the sprite faces right and left when i move the joystick around and i want it so it continues to face in the direction it last faced after i stop using the joystick.
    ideal: >> use joystick >>, << let go of joystick <<
    currently: >> use joystick >>, << let go of joystick >>
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    "the sprite faces right and left when i move the joystick around"

    So are you saying you have some sort of circular movement control instead of press up key, move up kind of setup?

    Edit: I'm asking this because you say "move joystick around" and you are using angle instead of vector2 for example...
     
  5. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    It's not blatantly apparent but I'm guessing that when you release the joystick, it resets to your default idle animation. Otherwise, it is resetting your image back to it's default state.

    You will need to go into your Joystick script to find this and fix it there. Now, if it's animations then you will probably need to create an idle animation for each possible direction and set its state in the same place within the Joystick script. Now if you're not using animations then you can probably remove the reset code upon joystick release and your player should remain facing that direction. Of course this is assuming you're not changing states.