Search Unity

flipping sprite

Discussion in '2D' started by ChikenMilk, Jul 15, 2018.

  1. ChikenMilk

    ChikenMilk

    Joined:
    Jun 11, 2018
    Posts:
    22
    I'm trying to make my plane flip on the x when it goes off the camera. But I can't figure off how to do it.
    Any help?
     
    Last edited: Jul 15, 2018
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    372
    get the spriterenderer.... spriterenderer.flipX = true;

    Personally I prefer to scale the X of the transform to the opposite because this way if you have child objects(other sprites like items) they flip as well.
     
  3. ChikenMilk

    ChikenMilk

    Joined:
    Jun 11, 2018
    Posts:
    22
    Yea, that works but how do I get the plane to flip when its out of the camera.
    here is a picture of the game if it helps.
    upload_2018-7-15_15-12-10.png
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You could add a trigger BoxCollider2D component on the Camera object that spans the entire area of the camera's view. Then, using the OnTriggerExit callback method, flip the sprite when the plane exits the collider.

    A rough example:
    Code (CSharp):
    1. void OnTriggerExit2D(Collider2D other) {
    2.    if(other.name == "Plane") {
    3.       SpriteRenderer otherRenderer = other.GameObject.GetComponent<SpriteRenderer>();
    4.       otherRenderer.flipX = !otherRenderer.flipX; //Sets "flipX" to the opposite of its current value
    5.    }
    6. }
     
  5. ChikenMilk

    ChikenMilk

    Joined:
    Jun 11, 2018
    Posts:
    22
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlaneFlipper : MonoBehaviour {
    4.  
    5.     void OnTriggerExit2D(Collider2D other)
    6.     {
    7.         if (other.name == "Plane")
    8.         {
    9.             SpriteRenderer otherRenderer = other.gameObject.GetComponent<SpriteRenderer>();
    10.             otherRenderer.flipX = !otherRenderer.flipX; //Sets "flipX" to the opposite of its current value
    11.         }
    12.     }
    13.  
    14. }
    15.  
    Doesn't work I don't know why?

    upload_2018-7-16_9-2-4.png

    I might be missing something I'm not the brightest person
     
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    The example I posted was meant to be attached to the main camera instead of the plane, but if you want it to be the other way around, you could do it like this instead (and I left some comments explaining what each line does this time):
    Code (CSharp):
    1. private SpriteRenderer spriteRenderer; //Create a reference to a SpriteRenderer component
    2.  
    3. void Start() {
    4.    spriteRenderer = GetComponent<SpriteRenderer>(); //Assign the reference to the SpriteRenderer component attached to this GameObject when the script starts
    5. }
    6.  
    7. //This method is called when this object exits a trigger Collider2D (make sure the collider's "Is Trigger" value is checked)
    8. void OnTriggerExit2D(Collider2D other) {
    9.  
    10.    //Check if the collider that this object just exited was attached to a GameObject named "Main Camera"
    11.    if(other.name == "Main Camera") {
    12.       spriteRenderer.flipX = !spriteRenderer.flipX; //Sets this SpriteRenderer's "flipX" value to the opposite of its current value
    13.    }
    14. }
    From the image you posted, it looks like you attached the Collider2D to a child object of the Main Camera.
    In this case, the if-statement in the OnTriggerExit2D method shouldn't be checking for the name of "Main Camera", rather it should be checking for the name (or tag, if you wish) of the child object of the Main Camera that has the Collider2D component.

    Maybe name the child object something like "Plane Flipper Collider" and check for this name instead of "Main Camera".
     
    nelloe88 likes this.
  7. ChikenMilk

    ChikenMilk

    Joined:
    Jun 11, 2018
    Posts:
    22
    I'm still confused.
    I'm very sorry for wasting your time. But I just don't get it it doesn't work I know I'm just not getting something.

    Thank you, for helping me even though you could just ignore this stupid forum.

    Can you try to explain it step by step or in a different way? I'm pretty sure its obvious but I'm still confused.
     
  8. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    Could you please explain exactly what is not functioning properly?
     
  9. ChikenMilk

    ChikenMilk

    Joined:
    Jun 11, 2018
    Posts:
    22
    I'm trying to make the plane flip when its out of the cameras view so the player does not see it flip.
     
  10. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Add a BoxCollider2D component to your Main Camera. Adjust the box size so it matches your camera view. If you added it to a child object under the Main Camera (it looks like you might have since the Main Camera game object has a hierarchy triangle next to it in your screenshot), then delete the child object and add the box collider directly to the Main Camera. Check the Trigger box on this component.

    Attach the script the Cucci_A wrote for you to your plane game object.

    When the plane leaves the box colliders bounds, the script should make the sprite flip. Add the following line at line 9 to see what might be happening:
    Debug.Log ("OnTriggerEnter2D called");
     
  11. ChikenMilk

    ChikenMilk

    Joined:
    Jun 11, 2018
    Posts:
    22
    Plane just keeps going doesn't flip :/
     
  12. ElnuDev

    ElnuDev

    Joined:
    Sep 24, 2017
    Posts:
    298
    Think over your project from basic principles. If you completely cannot find the problem, it can be helpful to start over.
     
  13. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    Did you get any logging in the console?

    Post your plane script code.
    Show a screen shot of your main camera inspector window making sure the collider component can be seen.
    Show a screen shot of your plane game object in the inspector window.
     
  14. netramz

    netramz

    Joined:
    May 22, 2018
    Posts:
    16
    Something to keep in mind is that if you do manage to flip the sprite of the plane, that does not necessarily mean that the plane will begin to move in the opposite direction. Another way of saying this is that the visuals of the plane and the physics of the plane are not always tied together.

    You say that the plane is not flipping; are you able to get the plane back into camera view without restarting your game? If not, then I think it would be helpful if you posted the movement script of the plane as @barskey suggests because @Cucci_A's suggestion may be working properly just without you being able to see it occur because nothing is telling the plane to switch its movement directions.
     
  15. ChikenMilk

    ChikenMilk

    Joined:
    Jun 11, 2018
    Posts:
    22
    I did this to do some testing

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlaneFlipper : MonoBehaviour
    4. {
    5.  
    6.     void OnTriggerExit2D(Collider2D other)
    7.     {
    8.         Debug.Log(other.gameObject.name);
    9.         Destroy(other.gameObject);
    10.         Debug.Log("?");
    11.     }
    12. }
    13.  
    And found out it detects the bombs not the plane.

    Anyone know why?
     
  16. ChikenMilk

    ChikenMilk

    Joined:
    Jun 11, 2018
    Posts:
    22
    I DID IT. I did

    plane = GameObject.Find("Plane"); and used it from there.

    Thank you guys so much for trying to help. I was wasting your life so this is the least I can do.

    THANK YOU!!!