• 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 LevitateWind · Aug 09, 2016 at 05:18 AM · 2d-platformer2d rotation

How to flip my characters direction while running, jumping etc. 2D

So pressing the "D" key my character runs to the right just fine but I want to make it so when you press the "A" key the character flips and then continues to to run and jump to the left.

if (GetComponent().velocity.x > 0)

         transform.localScale = new Vector3(1f, 1f, 1f);
     
     else if (GetComponent<Rigidbody2D>().velocity.x < 0)
     
         transform.localScale = new Vector3(-1f, 1f, 1f);

This little bit of code allows my character to flip left yes but whenever you move it at all he grows twice in size. It's kind of funny but, not totally what I'm going for. I realize this may just be a silly mistake but, anyone that can help out would be much appreciated! Thank you all in advance :D

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
Best Answer

Answer by ScaniX · Aug 09, 2016 at 02:48 PM

What is your initial size? Vector3(0.5f, 0.5f, 0.5f)?

You can probably use this:

private

 Rigidbody myRigidbody;
 
 void Start() {
     myRigidbody = GetComponent<Rigidbody>();
 }
 
 void Update() {
     if (myRigidbody.velocity.x != 0 && Mathf.Sign(myRigidbody.velocity.x) != Mathf.Sign(transform.localScale.x)) {
         Vector3 localScale = transform.localScale;
         localScale.x = Mathf.Sign(myRigidbody.velocity.x) * Mathf.Abs(localScale.x);
         transform.localScale = localScale;
     }
 }

Comment
Add comment · Show 3 · 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 LevitateWind · Aug 09, 2016 at 04:31 PM 0
Share

As soon as you asked about my initial size I realized that my scale was smaller than 1. So I was able to work it out. Thank you so much for the help mate!

avatar image TheShadyColombian LevitateWind · Aug 09, 2016 at 05:54 PM 0
Share

Please mark as correct to give more priority to unanswered questions

avatar image LevitateWind TheShadyColombian · Aug 09, 2016 at 06:18 PM 0
Share

Will do. Sorry about that.

avatar image
1

Answer by lunoland · Aug 09, 2016 at 04:59 PM

You might find this tidier:

 Rigidbody2D rigidbody;
 SpriteRenderer sprite;
 
 void Awake() {
     rigidbody = GetComponent<Rigidbody2D>();
     sprite = GetComponent<SpriteRenderer>();
 }
 
 void Update() {
     if (!Mathf.Approximately(rigidbody.velocity.x, 0f))
         sprite.flipX = rigidbody.velocity.x < 0f;
 }
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

(2D sidescrolling platformer) Flipping my character right or left depending on the mouse? 2 Answers

RotateAround using physics 1 Answer

2d shooting problem 1 Answer

2D Box Collider / Sprite change direction issue 1 Answer

Why wont this 2D sprite Flip when its rotation passes 90 & -90 degrees? 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