Search Unity

accessing the sprite renderer to flip the sprite? (C#) {SOLVED}

Discussion in '2D' started by milliehashh, Jan 25, 2016.

Thread Status:
Not open for further replies.
  1. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    Im currently working on a 2D platformer and was wondering if there is some sort if script i could use/write so that when the A key is pressed the 'flip X' box in the sprite renderer is set to true? i know i will have to reference the sprite renderer but I'm not sure how to do this :s
    I'm quite new to coding so any help is GREATLY appreciated
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You'll need to do something like this. This script will have to be on the same gameObject as the SpriteRenderer component.

    Make sure that the script name in the Editor is the same as the class name in the code.

    Code (CSharp):
    1. public class SpriteFlipper : MonoBehaviour
    2. {
    3.    // variable to hold a reference to our SpriteRenderer component
    4.    private SpriteRenderer mySpriteRenderer;
    5.  
    6.    // This function is called just one time by Unity the moment the component loads
    7.    private void Awake()
    8.    {
    9.         // get a reference to the SpriteRenderer component on this gameObject
    10.         mySpriteRenderer = GetComponent<SpriteRenderer>();
    11.    }
    12.  
    13.    // This function is called by Unity every frame the component is enabled
    14.    private void Update()
    15.    {      
    16.         // if the A key was pressed this frame
    17.         if(Input.GetKeyDown(KeyCode.A))
    18.         {
    19.             // if the variable isn't empty (we have a reference to our SpriteRenderer
    20.             if(mySpriteRenderer != null)
    21.             {
    22.                  // flip the sprite
    23.                  mySpriteRenderer.flipX = true;
    24.             }
    25.         }
    26.     }
    27. }
     
    Last edited: Dec 19, 2018
  3. milliehashh

    milliehashh

    Joined:
    Sep 17, 2015
    Posts:
    72
    This worked a charm, thank you! :D

    for anyone else that tries to use this method I had to add

    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.D))
    2.             {
    3.                 // flip the sprite
    4.                 mySpriteRenderer.flipX = false;
    5.             }
    in as well because if i tried to make the character walk in the opposite direction he ended up moonwalking haha.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I left that bit out hoping that you would expand on the solution on your own. Great job! Keep tinkering and editing and you'll be making your own scripts from scratch in no time.
     
    DigiGhost and milliehashh like this.
  5. zeref_slayer

    zeref_slayer

    Joined:
    Apr 4, 2020
    Posts:
    2
    Thanks for that code, it is really help me improve my coding skill
     
    LiterallyJeff likes this.
  6. jhadden04

    jhadden04

    Joined:
    Mar 29, 2020
    Posts:
    2
    its pretty useful, but if you press the opposite key while holding down the other key, it will mean you are facing the wrong way
     
  7. jhadden04

    jhadden04

    Joined:
    Mar 29, 2020
    Posts:
    2
    my imperfect solution
    if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") < 0)
    {
    // if the variable isn't empty (we have a reference to our SpriteRenderer
    if (mySpriteRenderer != null)
    {
    // flip the sprite
    mySpriteRenderer.flipX = true;
    }

    }
    //if (Input.GetKeyDown(KeyCode.D))
    if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") > 0)
    {
    // flip the sprite
    mySpriteRenderer.flipX = false;
    }
     
  8. PavanPenugonda

    PavanPenugonda

    Joined:
    Jul 14, 2020
    Posts:
    1
    Tq..Good work
     
  9. MSmigs1

    MSmigs1

    Joined:
    May 18, 2021
    Posts:
    1
    how do i make it so i can turn the other way around again
     
  10. Lolromantics

    Lolromantics

    Joined:
    May 16, 2021
    Posts:
    1
    Having a problem where sr.flipx causes other elements of my enemy script to fail
     
  11. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Please start your own thread for your NEW PROBLEM.

    When you post, keep this in mind:

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  12. plankjim3

    plankjim3

    Joined:
    Oct 10, 2021
    Posts:
    1
    Thanks a lot
     
Thread Status:
Not open for further replies.