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

2DSprite axis change for child objects when flipping

Discussion in '2D' started by EdoC-QWERTY, May 6, 2020.

  1. EdoC-QWERTY

    EdoC-QWERTY

    Joined:
    Feb 1, 2020
    Posts:
    75
    Hello,
    i just begin to use Unity and i am using 2D mode. I have a custom made sprite and i already made it flip when facing the opposite direction, but the problem is that, if i click the child object in the hierarchy window to see the axis, the axis does not change. I need the x axis of the child object to match the sprite facing so that it can shoot from that child object.

    I watched some videos about it but couldn't find a solution yet.

    Any ideas?

    Thanks in advance
     
  2. adilbdk

    adilbdk

    Joined:
    Apr 25, 2020
    Posts:
    3
    Are you using SpriteRenderer.flipX or transform.localScale to flip your object? If you are using Sprite renderer that might be why your objects arent flipping with your parent object.
     
  3. EdoC-QWERTY

    EdoC-QWERTY

    Joined:
    Feb 1, 2020
    Posts:
    75
    Here is my code
    "Player" is the name of the gameobject variable i created in this script.

    Code (CSharp):
    1.     public void Flip()
    2.     {
    3.       //float playerFaceAngle = Player.transform.eulerAngles.y;
    4.       facingRight = !facingRight;
    5.       //transform.eulerAngles = new Vector3(0,0,0);
    6.        //playerFaceAngle += 180f;
    7.  
    8.       Player.transform.Rotate(Vector3.up * 180);
    9.  
    10. // = new Vector3(0,playerFaceAngle,0);
    11.  
    12.     }
    When i first dragged my imported sprite into the scene it created a gameobject with a sprite renderer component, then i began to add components to that object like collider and rigidbody.
    Do i need to create a new empty gameobject and parent the sprite to it?

    EDIT: I tired to parent the sprite to another game object, using the same code the sprite is flipped in the scene but in the game it disappear and the axis doesn't change so..
     
    Last edited: May 6, 2020