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

Unable to rotate 2D player around Y axis

Discussion in '2D' started by PaulKhill, Dec 28, 2020.

  1. PaulKhill

    PaulKhill

    Joined:
    Jul 30, 2020
    Posts:
    15
    I'm trying to make my player character face the direction its walking toward in a 2D game. Supposedly simple. Yet I can't make it happen.

    The code looks like this :
    Code (CSharp):
    1. if (movement.x < Vector3.zero.x)
    2.             {
    3.                 Debug.Log("LEFT");
    4.                 transform.Rotate(new Vector3(0, 180,0));
    5.  
    6.             }
    7.             else
    8.             {
    9.                 Debug.Log("RIGHT");
    10.                 transform.Rotate(new Vector3(0, 0, 0));
    11.             }
    I've tried several version of this, including modifying rotate directly, as well as using LocalTransform. None of this seems to have any impact on my PC : there is absolutely no change on the editor or ingame. It detects the input (the Debug.log work) but no impact.

    Funnily enough, it works when my player swims, not when it walks. When it swims, i use this version (which doesn't work when I walk) :

    Code (CSharp):
    1.  
    2.  
    3.                 transform.localRotation = Quaternion.Euler(0, 180, -90);
    4.  
    Any idea of what I could be missing ?
     
    yash1905 likes this.
  2. raarc

    raarc

    Joined:
    Jun 15, 2020
    Posts:
    535
    use

    Code (CSharp):
    1. transform.localscale = new vector3(-1,1,1);
     
  3. PaulKhill

    PaulKhill

    Joined:
    Jul 30, 2020
    Posts:
    15
    Thanks raarc : it kind of works, but it's having cascading effects on children of the PC. I have a torch that follows the player's mouse, and using you method makes it work only when the player looks right. Anyway to make it work with rotate, so everything rotates ?
     
  4. eses

    eses

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

    Don't rotate your object... you are literally telling your object to "rotate" relative to current orientation. It is usually better to set rotation value to a specific value you want (or rotate towards this target value).
    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.Space))
    2. {
    3.     flipped = !flipped;
    4.  
    5.     if (flipped)
    6.         tra.rotation = Quaternion.Euler(0, -180, 0);
    7.     else
    8.         tra.rotation = Quaternion.Euler(0, 0, 0);
    9. }
    Although personally I would only make the sprite flip using SpriteRenderer's flipX toggle. But if you have a hierarchy of objects then you might have to move those by code to match flipped sprite, so in this case I guess rotating GameObject might work better for you.
     
    Last edited: Dec 28, 2020
    Ali_MAA and PaulKhill like this.
  5. PaulKhill

    PaulKhill

    Joined:
    Jul 30, 2020
    Posts:
    15
    Thanks for the answer, I'd have prefered to go with the flip method, but it'd just get messy with the childs indeed.

    I've already tried to set the rotation directly :

    Code (CSharp):
    1. if (movement.x < Vector3.zero.x)
    2.             {
    3.                 Debug.Log("LEFT");
    4.                 transform.rotation = Quaternion.Euler(0, 0, 0);
    5.  
    6.             }
    7.             else
    8.             {
    9.                 Debug.Log("RIGHT");
    10.                 transform.rotation = Quaternion.Euler(0, -180, 0);
    11.             }
    But for some reason, although it definitly reads it(DebugLog shows), it has no impact on the rotation of the object (the numbers aren't even switching in the editor). Any other cues ? What am I missing ?
     
  6. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    I tested it before pasting it here. The example I gave works.

    Maybe you are setting your rotation somewhere else too?
     
    PaulKhill likes this.
  7. PaulKhill

    PaulKhill

    Joined:
    Jul 30, 2020
    Posts:
    15
    Thanks, eses ! I was indeed setting my rotation elsewhere too (triggered by a collision that was too large for its own good).
     
    eses likes this.