• 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 nicoolsen10 · Apr 12, 2019 at 08:07 AM · 2dcharacterflip

Flip a 2d character

i want to flip my 2d character but i cant figure out how to do it can someone pls help me im new to programing so im trying to learn it

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour {

 public float moveSpeed;
 public float jumpForce;

 public KeyCode left;
 public KeyCode right;
 public KeyCode jump;
 public KeyCode shootBullet;

 private Rigidbody2D theRB;

 public Transform groundCheckPoint;
 public float groundCheckRadius;
 public LayerMask whatIsGrounded;

 public bool isGrounded;
 public bool Flip;

 private Animator anim;

 // Start is called before the first frame update
 void Start()
 {
     theRB = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
 }

 // Update is called once per frame
 void Update()
 {
     isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGrounded);

     if (Input.GetKey(left))
     {
         theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
         if (Flip == false)
         {
             Flip = true;
         }
     }
     else if (Input.GetKey(right))
     {
         theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
         if (Flip == true)
         {
             Flip = false;
         }
     }
     else
     {
         theRB.velocity = new Vector2(0, theRB.velocity.y);
     }

     if (Input.GetKeyDown(jump) && isGrounded)
     {
         theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
     }

     anim.SetFloat("Speed", Mathf.Abs(theRB.velocity.x));
     anim.SetBool("Grounded", isGrounded);
 }

 private void flip()
 {
     if (Flip == false)
     {
         transform.Rotate(0f, 0f, 0f);
     }

     if (Flip == true)
     {
         transform.Rotate(0f, 180f, 0f);
     }
 }

}

Comment
Add comment · Show 13
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 nicoolsen10 · Apr 12, 2019 at 09:34 AM 0
Share

Thanks for all the answers but none of them works like i want them to because i have a shoot point and it wont flip with the character

Show more comments

3 Replies

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

Answer by highpockets · Apr 12, 2019 at 08:33 AM

Ok, I edited your script. Now just make sure the transform is facing left at the start and it should work. If you are doing this with a 2D sprite on the other hand, just set the scale of the x value to -1 for left and 1 for right instead of rotate.

Cheers

     using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
     public class PlayerController : MonoBehaviour {
      public float moveSpeed;
      public float jumpForce;
     
      public KeyCode left;
      public KeyCode right;
      public KeyCode jump;
      public KeyCode shootBullet;
     
      private Rigidbody2D theRB;
     
      public Transform groundCheckPoint;
      public float groundCheckRadius;
      public LayerMask whatIsGrounded;
     
      public bool isGrounded;
      public bool facingLeft = true;
     
      private Animator anim;
     
      // Start is called before the first frame update
      void Start()
      {
          theRB = GetComponent<Rigidbody2D>();
          anim = GetComponent<Animator>();
      }
     
      // Update is called once per frame
      void Update()
      {
          isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, groundCheckRadius, whatIsGrounded);
     
          if (Input.GetKey(left))
          {
              theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
              if (facingLeft == false)
              {
                  transform.Rotate(0,180,0);
                  facingLeft = true;
              }
          }
          else if (Input.GetKey(right))
          {
              theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
              if (facingLeft == true)
              {
                  transform.Rotate(0,-180,0);
                  facingLeft = false;
              }
          }
          else
          {
              theRB.velocity = new Vector2(0, theRB.velocity.y);
          }
     
          if (Input.GetKeyDown(jump) && isGrounded)
          {
              theRB.velocity = new Vector2(theRB.velocity.x, jumpForce);
          }
     
          anim.SetFloat("Speed", Mathf.Abs(theRB.velocity.x));
          anim.SetBool("Grounded", isGrounded);
      }
     } 

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 shgdi · Apr 12, 2019 at 08:24 AM

In need yo use this code SpriteRenderer.flipX to flip your 2d sprite. So go ahead and change it. Do something like this :

  private void flip()
  {
      if (Flip == false)
      {
         GetComponent<SpriteRenderer>().flipX =false;
      }
      if (Flip == true)
      {
          GetComponent<SpriteRenderer>().flipX = true;
      }
  }
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 Kaihyo · Apr 12, 2019 at 08:45 AM

Hi !

Actually, you do not call your flip() method anywhere in your update loop. You may try the following :

 if (Input.GetKey(left))
 {
     theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
     if (!Flip) // same as Flip == false
     {
         flip();
     }
 }
 else if (Input.GetKey(right))
 {
     theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
     if (Flip) // same as Flip == true
     {
         flip()
     }
 }
 else
 {
     theRB.velocity = new Vector2(0, theRB.velocity.y);
 }

Your flip() method should look like this.

 private void flip()
 {
     // in both case, we rotate it by 180° around the Y-axis
     transform.Rotate(0f, 180f, 0f);
     // flip takes its opposite value (if it was flipped, it becomes unflipped and vice versa)
     flip != flip;
 }
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

235 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 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 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 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 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 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 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 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 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

2D Character Flip 2 Answers

2D example: Flip character without moving 0 Answers

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

Flat 2d image on 3d character. 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