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

Flipping 2d character localscale *= -1

Discussion in '2D' started by weareallthereis, Sep 8, 2018.

  1. weareallthereis

    weareallthereis

    Joined:
    May 17, 2015
    Posts:
    8
    I have code that looks something like this:
    Code (CSharp):
    1. facingRight = !facingRight;
    2.             Vector3 theScale =  this.gameObject.transform.localScale;
    3.             theScale.x *= -1;
    4.             this.gameObject.transform.localScale = theScale;
    but it doesnt flip the enemy character accordingly. I have code:
    Code (CSharp):
    1. Debug.Log(this.gameObject.transform.position.ToString());
    2.             Debug.Log(player.position.x.ToString());
    3.             Debug.Log(facingRight);
    and they are printing the correct values but I don't think the
    Code (CSharp):
    1. this.gameObject.transform.localScale = theScale;
    is working.
     
  2. MrZzzero

    MrZzzero

    Joined:
    Dec 9, 2016
    Posts:
    67
    didn't read the code but u can use flip option in sprite renderer
     
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I'm assuming that you're trying to get your sprite to flip in the opposite direction, correct?

    My 2D movement is based upon "Horizontal", so a player can use "A" , "D" , "LeftArrow", or "RightArrow". What I did is determine which key was being used and added the sprite desired inside there.

    Code (CSharp):
    1. void Update () {
    2.  
    3.         transform.Translate(Pspeed * Input.GetAxis("Horizontal") * Time.deltaTime,0f,0f);
    4.  
    5.         if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) {
    6.            GetComponent<Renderer>().material = rgtFace;
    7.             gun.SetActive(false);
    8.             gun1.SetActive(true);
    9.          
    10.         }
    11.         if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
    12.         {
    13.            
    14.             GetComponent<Renderer>().material = lftFace;
    15.             gun.SetActive(true);
    16.             gun1.SetActive(false);
    17.          
    18.         }
    19.  
    20.      
    21.  
    22.     }
    Edit: Here's an example. basically I created two materials to use as a sprite. Then I duplicated my weapon and faced it the other way. I set the weapon's active or inactive status within the proper key call. I done the same thing with my materials. And could do the same IF I was doing animations, which in this instance I am not. Also note that I accidentally set my left and right face materials into the wrong slots inside unity. But it works and I don't see the point in changing it lol
     
    Last edited: Sep 9, 2018
  4. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    maybe have to set new Vector3 for scale instead of setting one of the Vector3 properties

    foo.scale = new Vector3(oldx, oldy, newz);
     
  5. weareallthereis

    weareallthereis

    Joined:
    May 17, 2015
    Posts:
    8
    I dont see you changing the scale but I tried GetComponent<Renderer>() to alter values and nothing

    didnt work, I tried manually inputting values new Vector3(-7,7,3);
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I defined two variables:
    Code (CSharp):
    1. public Material leftFace;
    2. public Material rightFace;
    3. // Yes I fully spelled out the variables // here but not in my code I // provided earlier. This is for clarity.
    I designed two textures, not sprites of my character facing left/right. I dropped them into the inspector so the script could easily access them. My movement is based upon horizontal X-Axis, however, you still must press a movement key in order to move. So I added the GetKeyDown to determine which way I was facing and I rendered the proper material. That's how I changed the scale. However, I'm using raycasting to detect walls that I sometimes can walk through for various reasons. So I implemented the GetAxis movement code inside of each key-check and use GetKey instead of GetKeyDown now.

    However, you should be able to accomplish the same thing with a sprite animation which wouldn't require GetComponent<Render>(). You'd just put your animation or image you you're using and grab that.