Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 LightHades · Aug 27, 2020 at 05:20 AM · animationjumpflipping

Jump animation won't flip mid-air

I've been trying to get my animations to work, and they are, except there's one issue: the jump animation won't flip (along the X axis) whilst mid-air. If you land and jump again and you change the direction, it flips, but not during the jump. (All my other animations are working fine and flip when required). Here's my code:

using UnityEngine;

public class CharacterMovement : MonoBehaviour {

 public float moveSpeed = 1;
 public float jumpForce = 1;
 private Rigidbody2D rgbd;
 private Animator anim;
 private SpriteRenderer sr;
 private void Start()
 {
     rgbd = GetComponent<Rigidbody2D>();
     anim = gameObject.GetComponent<Animator>();
     sr = GetComponent<SpriteRenderer>();
 }


 private void Update()
 {
     var movement = Input.GetAxis("Horizontal");
     transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * moveSpeed;

     if (movement > 0 && Mathf.Abs(rgbd.velocity.y) < 0.001f)
     {
         anim.Play("Run");
         sr.flipX = false;
     }
     
     else if (movement < 0 && Mathf.Abs(rgbd.velocity.y) < 0.001f)
     {
         anim.Play("Run");
         sr.flipX = true;
     }
     
     else if (movement == 0 && Mathf.Abs(rgbd.velocity.y) < 0.001f)
     {
         anim.Play("Idle");
     }

     if (Input.GetButtonDown("Jump") && Mathf.Abs(rgbd.velocity.y) < 0.001f)
     {
         rgbd.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
         anim.Play("Jump");
     }

     if (Input.GetButtonDown("Jump") && Mathf.Abs(rgbd.velocity.y) < 0.001f)
     {
         rgbd.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
         anim.Play("Jump");
     }
 }

}

I'm a beginner, so I would appreciate if someone could help!

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 Chrissyeah_ · Aug 27, 2020 at 09:49 AM

 if (movement > 0 && Mathf.Abs(rgbd.velocity.y) < 0.001f)
      {
          anim.Play("Run");
          sr.flipX = false;
      }

Here you are limiting this loop only to when there is no velocity on the y axis, which means if your y velocity is greater than 0.001f you cant flip.

Instead you could.

 if (movement > 0)
      {
          if (Mathf.Abs(rgbd.velocity.y) < 0.001f)
          {
               anim.Play("Run");    
          }
          sr.flipX = false;
      }

But I would suggest rather using a collision check to see if touching the ground, this has just worked more dynamically for me.

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 LightHades · Aug 27, 2020 at 12:53 PM

Hey there! Thanks a lot, it worked, I didn't realise the fix was so simple! I'm not too familiar with the collision check, however, I'll look into it and see what I can find. Thanks again!

Comment
Add comment · Show 4 · 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 Llama_w_2Ls · Aug 27, 2020 at 02:16 PM 0
Share

The collision check uses an empty GO beneath the player's feet and uses Physics.CheckSphere() to create a small sphere around the empty GO, and if it is intersecting with the ground layer, then youre grounded, else, youre jumping or falling.

avatar image LightHades Llama_w_2Ls · Aug 27, 2020 at 03:46 PM 0
Share

So is it similar to a groundcheck?

avatar image Llama_w_2Ls LightHades · Aug 27, 2020 at 03:48 PM 0
Share

Yes thats exactly what it is basically

Show more comments

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

332 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Forward jumping 1 Answer

Can I make animations snap to a frame? 1 Answer

How come my player animation won't play? 1 Answer

How can I activate my jump script only at my crouch animation ? 0 Answers

Increase jump height/distance 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