Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ZushidotomoThe3DHero · Sep 05, 2015 at 03:18 PM · 2d2d gamecharacterfliparrow-keys

Flip 2D Character with Keys without moving

Hey, guys! I got into Unity really recently and I have been working on a simple game. So simple, I don't even want to move my character throughout the level. I just need to make him flip everytime I press the Left and Right arrow keys. I've been learning some stuff here and there and searching everywhere but it's not enough for me to understand this yet, so I needed your help! I'm sorry if I look like I'm making you do all the work, it's not my wish to do so! I have this piece of code but I need to know where to change exactly so I can use the arrow keys and make him JUST flip and not move.

 using UnityEngine;
 using System.Collections;
 
 public class FlipCharacter : MonoBehaviour {
     
     public float maxSpeed = 1f;
     bool facingRight = true;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         float move = Input.GetAxis ("Horizontal");
 
 
         
         GetComponent<Rigidbody2D>().velocity = new Vector2 (move * maxSpeed,  GetComponent<Rigidbody2D>().velocity.y);
 
 
         if(move > 0 && !facingRight)
         {
             Flip ();
         }
         else if(move < 0 && facingRight)
         {
             Flip ();
         }
     }
     
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }


If you need anything more, tell me! Thanks in advance!

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

Answer by Serdan · Sep 07, 2015 at 11:47 PM

From what I can tell it should already work, though I would have done it differently. What happens when you use it?

It's generally best to use Input.GetAxis(), but if you just want it to work you can use the following and expand on that:

  using UnityEngine;
  using System.Collections;
  
  public class FlipCharacter : MonoBehaviour
 {
     void FixedUpdate()
     {
         if (Input.GetKeyDown(KeyCode.LeftArrow))
         {
             SetDirection(-1);
         }
         if(Input.GetKeyDown(KeyCode.RightArrow))
         {
             SetDirection(1);
         }
     }
 
     void SetDirection(float direction)
     {
         Vector3 theScale = transform.localScale;
         theScale.x = direction;
         transform.localScale = theScale;
     }
 }

If this doesn't work it's because you've set something up wrong on the Unity side. Note that left may be right and vice versa. Play around with the values passed to SetDirection() if that is the case.

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 ZushidotomoThe3DHero · Sep 08, 2015 at 01:17 PM 0
Share

It worked, dude! God bless you, thank you so much!! Really!!

avatar image
1

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

Hey, I just found this way, it is simple and works like a charm!

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

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 ZushidotomoThe3DHero · Sep 07, 2015 at 07:57 PM 0
Share

Thank you! But what am I supposed to do? Add it or use this one ins$$anonymous$$d of all the other code i posted here?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I make my player flip to always face the enemy and vice versa? 1 Answer

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

Flip the player with arm rotation 2 Answers

2D example: Flip character without moving 0 Answers

2D Character Flip 2 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