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?
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.
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.
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): void OnTriggerExit2D(Collider2D other) { if(other.name == "Plane") { SpriteRenderer otherRenderer = other.GameObject.GetComponent<SpriteRenderer>(); otherRenderer.flipX = !otherRenderer.flipX; //Sets "flipX" to the opposite of its current value } }
Code (CSharp): using UnityEngine; public class PlaneFlipper : MonoBehaviour { void OnTriggerExit2D(Collider2D other) { if (other.name == "Plane") { SpriteRenderer otherRenderer = other.gameObject.GetComponent<SpriteRenderer>(); otherRenderer.flipX = !otherRenderer.flipX; //Sets "flipX" to the opposite of its current value } } } Doesn't work I don't know why? I might be missing something I'm not the brightest person
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): private SpriteRenderer spriteRenderer; //Create a reference to a SpriteRenderer component void Start() { spriteRenderer = GetComponent<SpriteRenderer>(); //Assign the reference to the SpriteRenderer component attached to this GameObject when the script starts } //This method is called when this object exits a trigger Collider2D (make sure the collider's "Is Trigger" value is checked) void OnTriggerExit2D(Collider2D other) { //Check if the collider that this object just exited was attached to a GameObject named "Main Camera" if(other.name == "Main Camera") { spriteRenderer.flipX = !spriteRenderer.flipX; //Sets this SpriteRenderer's "flipX" value to the opposite of its current value } } 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".
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.
I'm trying to make the plane flip when its out of the cameras view so the player does not see it flip.
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");
Think over your project from basic principles. If you completely cannot find the problem, it can be helpful to start over.
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.
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.
I did this to do some testing Code (CSharp): using UnityEngine; public class PlaneFlipper : MonoBehaviour { void OnTriggerExit2D(Collider2D other) { Debug.Log(other.gameObject.name); Destroy(other.gameObject); Debug.Log("?"); } } And found out it detects the bombs not the plane. Anyone know why?
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!!!