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
2
Question by deamo · Mar 20, 2014 at 04:31 PM · 2dplayercharacterdirectionflip

Flip 2D player in x-axis to face movement direction

Hi!

I'm new to unity and the game world, but I have always wonder how it would be to build a game so I have started out with a 2D game :)

So, I have a player gameobject that use collider and rigidbody2d to move around (yes, no really control over the player object). He bounces around and I would like him to turn/flip into the direction he is moving on the x-axis. This is the code I have so far. It works when he moves right, but when he moves left he flips back and forth between facing left&right.

I have attached this script to my player:

 void Start () 
     {
         direction = 1;
         _posX = transform.position.x;
 
         Debug.Log ("Position X: " + _posX);
 
     }
 
     void Update()
     {
         if (transform.position.x < _posX)
         {
             Debug.Log("Moving left - " + transform.position.x);
             if (direction == 1)
             {
                 transform.localScale = new Vector2(-1, transform.localScale.y);
                 direction = -1;
             }
         }
         else
         {
             Debug.Log("Moving right - " + transform.position.x);
             if (direction == -1)
             {
                 transform.localScale = new Vector2(1, transform.localScale.y);
                 direction = 1;
             }
         }
 
         _posX = transform.position.x;
     }

Hope someone can help me to figure out what is wrong :)

Regards Stellan

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

5 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by koray1396 · Mar 20, 2014 at 06:59 PM

in your if statement, when velocity.x < 0, you flip your character every frame by, so it flips back and forth.

 transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y);

a simple way to fix it is;

 float someScale;
 
 void Start(){
    someScale = transform.localScale.x; // assuming this is facing right
 }
 
 // replace transform.localScale = new Vector2(transform.localScale.x, transform.localScale.y); by
 transform.localScale = new Vector2(someScale, transform.localScale.y);
 // and...        transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y); by
         transform.localScale = new Vector2(-someScale, transform.localScale.y);
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 deamo · Mar 20, 2014 at 09:01 PM 0
Share

Thanks alot! Works perfectly!

How come that I have to set the someSmale in the start event? Why can't transform.localScale.x be called in the Update event directly?

avatar image maravillart · Oct 02, 2019 at 04:29 PM 0
Share

I been trying to flip my player so many Codes from tutorial on youtube. The only Answer that work was yours. Thank You!! Thank You !!

avatar image
0

Answer by swag_swag · Mar 20, 2014 at 05:18 PM

,Simply try changing the (direction = -1) for "Moving left - " to a -1. Or perhaps direction = 1` void Start () { direction = -1; _posX = transform.position.x;

 Debug.Log ("Position X: " + _posX);
  
 }
  
 void Update()
 {
 if (transform.position.x < _posX)
 {
 Debug.Log("Moving left - " + transform.position.x);
 if (direction == -1)
 {
 transform.localScale = new Vector2(-1, transform.localScale.y);
 direction = -1;
 }
 }
 else
 {
 Debug.Log("Moving right - " + transform.position.x);
 if (direction == -1)
 {
 transform.localScale = new Vector2(1, transform.localScale.y);
 direction = 1;
 }
 }
  
 _posX = transform.position.x;
 }`
Comment
Add comment · 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
0

Answer by deamo · Mar 20, 2014 at 06:50 PM

I have updated the code a bit. More correct way to determine what way he is moving on the x-axis, but he still starts to flip back and forth as soon as he moves left :(

Can it have something to do with my collider?

     void Start () 
     {
         Debug.Log (rigidbody2D.velocity.x);
     }
 
     void Update()
     {
         if (rigidbody2D.velocity.x >= 0)
         {
             Debug.Log("Moving right " + rigidbody2D.velocity.x);
             transform.localScale = new Vector2(transform.localScale.x, transform.localScale.y);
         }
         else
         {
             Debug.Log("Moving left " + rigidbody2D.velocity.x);
             transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y);
         }
     }
 
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 koray1396 · Mar 28, 2014 at 04:43 PM 0
Share

the following code runs on every update.

 transform.localScale = new Vector2(-transform.localScale.x, transform.localScale.y);

this is continuously flipping the gameobject. -transform.localScale.x in this line means the reverse of CURRENT localScale.x NOT the reverse of starting value of localScale.x.

Simply, it looks at the gameobject and says "well this is now 1, it should be -1". In the next frame, "now this is -1, and i am making it 1".

Apologies for putting it like this, I couldn't figure out any other way to describe it. But you should understand the difference;

 void Start(){
    someValue = transform.position;
 }
 
 void Update(){
    someValue2 = transform.position;
 }

someValue will give you the starting position and will not change unless you put another line. someValue2 will give you the current position, and this will run on every update, means, this value will be updated in regards to the object.

I hope this is explaining for you and the issue is clarified.

Nice days!

avatar image
0

Answer by Fubiou · Sep 07, 2015 at 07:32 AM

Hey, I found a simple way to flip the 2d character with a simple line:

transform.localEulerAngles = transform.eulerAngles + Vector3(0,180,-2*transform.eulerAngles.z);

Comment
Add comment · 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
0

Answer by kakubeiUK · Feb 02, 2019 at 03:38 PM

What's wrong with flipX = true;?

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 Nyxas · Feb 02, 2019 at 05:40 PM 0
Share

I think it's because the flipX only works for the sprite and not for the colliders, so the scale is changed to maintain the relationships. But in the platform game I use the FlipX and it worked perfectly, besides the code is very clean Example: if (inputHorizontal > 0) sprite.flipX = false; else if (inputHorizontal < 0) sprite.flipX = true;

avatar image dchaverri · Apr 24, 2020 at 09:04 AM 0
Share

Just wanted to add that flipX won't work if you have any child game objects as well :)

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Flip 2D Character with Keys without moving 2 Answers

Flip the player with arm rotation 2 Answers

How can I move the character like 2D games? 1 Answer

how to build a 2d character which can faced 2 directions(left and right), with high performance 1 Answer

2D example: Flip character without moving 0 Answers


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