Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DarkArtsProdigy · Oct 06, 2013 at 10:48 PM · c#

Flipping a 2D GameObject

I have this script to make a 2D sprite walk left to right but when he turns left, he won't flip. Can anyone help me make him flip when he turns left.

     public float walkSpeed = 2.0f;
     public float wallLeft = 0.0f;
     public float wallRight = 5.0f;
  
     float walkingDirection = 1.0f;
     Vector3 walkAmount;
  
     // Update is called once per frame
     void Update () {
  
         walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
  
         if (walkingDirection > 0.0f && transform.position.x >= wallRight)
             walkingDirection = -1.0f;
         else if (walkingDirection < 0.0f && transform.position.-x  <= wallLeft)
             walkingDirection = 1.0f;
  
         transform.Translate(walkAmount);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Oct 06, 2013 at 11:19 PM

A solution is to change the texture scale.

 renderer.material.SetTextureScale ("_MainTex", Vector2(-1,1));

Switch the sign on the first parameter of the Vector2 to reverse the image.

You can also use a shader with culling turned off and just rotate the object:

 transform.eulerAngles = Vector3(0.0, 180.0, 0.0);

If you don't turn off culling, the object will disappear.

If you are going to have a lot of these objects, and you are going to use a texture atlas, a good way to flip is to change the uv coordinates in the mesh.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image partidosolo · Oct 06, 2020 at 02:07 PM 0
Share

It doesen't work for me. I am on Unity 2019

avatar image
0

Answer by Hoeloe · Oct 06, 2013 at 10:56 PM

Well, that would be because none of that code has actually told it to flip around. All you're working with here is transform.Translate, which is entirely to do with movement (translation is a mathematical term for moving something). What you need to do is to change the scale of the character. It's quite a simple logical step to consider that if a scale of 1 in a certain axis is facing one way, then a scale of -1 in that axis will create the mirror image. All you need to do is to find the correct axis (most likely the z or x axis) in transform.localScale, and multiply it by -1.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Oct 06, 2013 at 11:15 PM 0
Share

@Hoeloe - workable solution, but I believe this plays havoc when colliders are involved.

avatar image Hoeloe · Oct 06, 2013 at 11:20 PM 0
Share

That depends how you do it. If your collider or plane is not symmetric, then changing the scale of the texture plane without the collider will break things, though you can always enclose these inside a parent GameObject for the purpose of changing the scale.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mac App Sandbox Adding Capabilities 0 Answers

Unity 4.5 No Height map Button? 1 Answer

Help Please i have a Error saying CSharp-firstpass.dll.mdb" is denied. 0 Answers

Builds won't run at all 3 Answers

How to do slowly rising bar? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges