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 Salacious · Sep 14, 2014 at 10:17 PM · animationanimatorplatformercharacter controller2d animation

Why is my character not flipping?

I am trying to learn the basics of Unity so I downloaded the latest free version. I created a little place for my character to walk around and a little character (most of which I borrowed from RPG Maker as I am no artist). With that said I created Added a idol and walking animations and hooked them into the animator.

Then I wrote the following:

 using UnityEngine;
 using System.Collections;
 
 public class character_controller_script : MonoBehaviour {
 
     public float maxSpeed = 10f;
     private bool facingRight = true;
 
     Animator anim;
 
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator> ();
     
     }
     
     // Update is called once per frame
     void FixedUpdate () {
 
         // Move at a speed that is predetermined on the Y axis.    
         float move = Input.GetAxis ("Horizontal");
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
 
         anim.SetFloat ("Speed", Mathf.Abs (move));
 
         if (move > 0 && !facingRight) {
             Flip();
         } else if(move < 0 && !facingRight) {
             Flip();
         }
     }
 
     // Flips the character based on input.
     void Flip() {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }
 

Which is pretty basic. We can all understand whats going on here. The issue is simple: The character will move forwards, the character will move backwards, the animations are all linked and work as expected but the character will not flip. So when I move right, he walks. When I move left, it looks like he is walking backwards....

Did I do something wrong in the flip function?

I was watching this tutorial on character control, thats where I got the code from. When he imported his characters they were all right facing, and as far as I understand the code should just flip them for you.

How do I make it flip?

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

Answer by tanoshimi · Sep 14, 2014 at 10:20 PM

Your code always makes the character face right because you have a logic error. I suspect line 28 should say:

 } else if(move < 0 && facingRight) {
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
1

Answer by TheXWolf · Sep 14, 2014 at 11:59 PM

The Flip() method flips the character to face the proper direction. First make sure you begin the scene with the player facing the right way.

Also if move is less than 0 if(move < 0) than the character & the character isn't facing right; !facingRight than it's already pointing the right way. The correct code should be:

 if(move < 0 && facingRight)
 {
   Flip();
 }
 else if(move > 0 && !facingRight)
 {
   Flip();
 }
 
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 kimn_unity · Aug 19, 2020 at 08:55 PM 0
Share

Well. $$anonymous$$e too. $$anonymous$$y character isn't flipping. I wrote: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class PlayerController : $$anonymous$$onoBehaviour
 {
     public float speed;
     public float jumpForce;
     private float moveInput;
 
     private Rigidbody2D rb;
 
     private bool facingRight = true;
 
     private bool isGrounded;
     public Transform groundCheck;
     public float checkRadius;
     public Layer$$anonymous$$ask whatIsGround;
 
 
     private int extraJumps;
     public int extraJumpsValue;
 
     private Animator anim;
 
     void Start()
     {
         anim = GetComponent<Animator>();
         extraJumps = extraJumpsValue;
         rb = GetComponent<Rigidbody2D>();
     }
 
     void FixedUpdate(){
 
         isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
 
 
         moveInput = Input.GetAxis("Horizontal");
         rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
 
         if (moveInput == 0)
         {
             anim.SetBool("isRunning", false);
         } else
         {
             anim.SetBool("isRunning", true );
         }
 
         if(moveInput < 0 && facingRight)
         {
             Flip();
             
             } else if (moveInput > 0 && !facingRight)
         {
             Flip();
         }
     }
 
 
     void Update(){
         if(isGrounded == true){
             extraJumps = extraJumpsValue;
             anim.SetBool("isJumping", false);
 
 
         }
         else
         {
             anim.SetBool("isJumping", true);
         }
 
 
 
 
         if (Input.GetKeyDown(KeyCode.W) && extraJumps > 0)
         {
             anim.SetTrigger("takeOf");
             rb.velocity = Vector2.up * jumpForce;
             extraJumps--;
         } else if(Input.GetKeyDown(KeyCode.W) && extraJumps == 0 && isGrounded == true){
             rb.velocity = Vector2.up * jumpForce;
         }
         if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0)
         {
             anim.SetTrigger("takeOf");
             rb.velocity = Vector2.up * jumpForce;
             extraJumps--;
         }
         else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true)
         {
             rb.velocity = Vector2.up * jumpForce;
         }
 
     }
 
    
 
 
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 Scaler = transform.localScale;
         Scaler.x *= -1;
         transform.localScale = Scaler; 
     }
 }
 

but nope it didn't worked

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

problem with animations running then jumping 3 Answers

change the wrap mode of a 2d animation 0 Answers

2d and character controler 0 Answers

How to optimize 2D Animations created with 2D Animation packege? (v3.1.1) 0 Answers

Animator Condition Not Working 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