• 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 FairySeraph · Oct 01, 2017 at 02:58 PM · animationflipreverse

Flip Running Animation?

I'm having a bit of trouble trying to reverse the running animation of my character sprite. The thing that confuses me is that it reverses the idle standing animation, as well as the jumping animation. It just won't do the same for the running animation. When running left, it just holds the idle animation. So, why is it selective? Why won't it play a reverse running animation, yet plays everything else?

Here is the movement control script.

 Animator anim;
 public Rigidbody2D rb;
 public int playerSpeed = 10;
 public bool facingRight = true;
 private float moveX;
 public bool grounded = false;
 public Transform groundCheck;
 float groundRadius = 0.2f;
 public LayerMask whatIsGround;

 void Update()
 {
     rb = GetComponent<Rigidbody2D>();
     PlayerMove();
     anim = GetComponent<Animator>();
     grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
     anim.SetBool("Ground", grounded);
     anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
     float move = Input.GetAxis("Street");
     anim.SetFloat("Speed", move);
 }

 void PlayerMove()
 {

     float moveX = Input.GetAxis("Street");

     if (moveX < 0.0f && facingRight == false)
     {
         FlipPlayer();
     }
     else if (moveX > 0.0f && facingRight == true)
     {
         FlipPlayer();
     }
     gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);

 }


 void FlipPlayer()
 {
     facingRight = !facingRight;
     Vector2 localScale = transform.localScale;
     localScale.x *= -1;
     transform.localScale = localScale;
     anim.GetComponent<Animator>();
     float move = Input.GetAxis("Street");
     anim.SetFloat("Speed", move);
 }

And, here is the jumping script.

 public int JumpPower = 1250;
 //public Rigidbody2D rb;

 void Update()
 {
     //rb = GetComponent<Rigidbody2D>();
     PlayerMove();
 }

 void PlayerMove()
 {
     if (Input.GetButtonDown("Ups"))
     {
         Jump();
     }
 }

 void Jump()
 {
     GetComponent<Rigidbody2D>().AddForce(Vector2.up * JumpPower);
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FairySeraph · Oct 01, 2017 at 09:44 PM

So, I figured it out. Basically, I did some patchwork from other scripts I made where the animation worked, but the jumping didn't. Here are the two scripts, for running/animation, and for jumping, if anyone needs a reference like me. Couple them on your player. It's bloated and rough, but it at least seems to work.

 Animator anim;
 public Rigidbody2D rb;
 public int playerSpeed = 10;
 public bool facingRight = true;
 //public int playerJumpPower = 1250;
 private float moveX;
 public bool grounded = false;
 public Transform groundCheck;
 float groundRadius = 0.2f;
 public LayerMask whatIsGround;

 void Update()
 {
     rb = GetComponent<Rigidbody2D>();
     PlayerMove();
     anim = GetComponent<Animator>();
     grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
     anim.SetBool("Ground", grounded);
     anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
     float move = Input.GetAxis("Street");
     anim.SetFloat("Speed", move);

     float moveHorizontal = Input.GetAxis("Street");

     Flip(moveHorizontal);

     anim.SetFloat("Speed", Mathf.Abs(moveHorizontal));
 }

 void PlayerMove()
 {

     float moveX = Input.GetAxis("Street");

     if (moveX < 0.0f && facingRight == false)
     {
         FlipPlayer();
     }
     else if (moveX > 0.0f && facingRight == true)
     {
         FlipPlayer();
     }
     gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);

 }


 void FlipPlayer()
 {
     facingRight = !facingRight;
     Vector2 localScale = gameObject.transform.localScale;
     localScale.x *= -1;
     transform.localScale = localScale;
     //anim.GetComponent<Animator>();
     //float move = Input.GetAxis("Street");
     //anim.SetFloat("Speed", move);
 }

 private void Flip(float horizontal)
 {
     if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
     {
         facingRight = !facingRight;
         Vector2 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

And, for jumping.

 Animator anim;
 public Rigidbody2D rb;
 bool grounded = false;
 public Transform groundCheck;
 float groundRadius = 0.2f;
 public float jumpForce = 1000;
 public LayerMask whatIsGround;
 //private float verticalVelocity;
 //private float gravity = 14.0f;
 //public float jumpSpeed = 10;

 void Start()
 {
     rb = GetComponent<Rigidbody2D>();
     anim = GetComponent<Animator>();
 }

 void Update()
 {
     grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
     anim.SetBool("Ground", grounded);
     anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);
     PlayerMove();
 }

 void PlayerMove()
 {
     if (grounded && Input.GetButtonDown("Ups"))
     {
         Ups();
     }
 }

 void Ups()
 {
     rb.velocity = new Vector2(0, 20);
 }
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

178 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

Related Questions

importing spline Ik handle animation from maya into unity 0 Answers

How to loop an animation in reverse? 0 Answers

Animation reverse 1 Answer

Flip back a frame 1 Answer

How to play an animation in reverse? 1 Answer


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