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 DissonanceMask · Feb 11, 2020 at 04:57 PM · 2d gamejumpfightingame

How to make Player jump (like in a 2D fighting game)

I'm trying to make a 2D fighting game prototype, but im struggling with making the character jump while it has horizontal velocity. Everytime I keep pressing the jump button, it just makes him jump directly upwards or neutral jump. I believe it is because of the if statements, being grounded/recovered. Im not too sure what I can do to make the jump work as intended. Here is my code:

 public class FighterControl : MonoBehaviour
 {
     Rigidbody2D rb;
 
     float moveX;
     float jumpY;
         
     [SerializeField] float jumpX = 2.5f;
     [SerializeField] float moveSpeed = 2f;
     [SerializeField] float jumpForce;
     [SerializeField] bool recovered = true;
     [SerializeField] bool isGrounded;
     [SerializeField] Transform enemy;
     
     bool hasX;
     // Start is called before the first frame update
     void Awake()
     {
         rb = GetComponent<Rigidbody2D>();
         recovered = true;
     }
 
     // Update is called once per frame
     void Update()
     {
         moveX = Input.GetAxisRaw("Horizontal");
         jumpY = Input.GetAxisRaw("Vertical");
 
         //Positions and States
         FaceTowardsOpponent();
         isGrounded = IsGrounded();
     }
 
 
     private void FixedUpdate()
     {
         Walk();
         Jump();
     }
 
     private void FaceTowardsOpponent()
     {
         if (!recovered) { return; }
         else
         {
             transform.localScale = new Vector2(EnemyPosition(), 1f);
         }
     }
 
     void Walk()
     {
         if (!recovered || !IsGrounded()) { return; }
 
         else if (recovered && IsGrounded())
         {
             hasX = Mathf.Abs(moveX) > Mathf.Epsilon;
             Animator myAnim = GetComponent<Animator>();
             myAnim.SetBool("Walking", hasX);
             if (Mathf.Abs(moveX) > Mathf.Epsilon)
             {
                 rb.velocity = new Vector2(moveX * moveSpeed, rb.velocity.y);
             }
         }
     }
 
     void Jump()
     {
         Animator myAnim = GetComponent<Animator>();
         myAnim.SetBool("Grounded", IsGrounded());
         
         if(!IsGrounded()) { return;}
 
         if (Mathf.Abs(jumpY) > Mathf.Epsilon)
         {
             rb.velocity = new Vector2(moveX * jumpX, jumpForce);
         }
     }
     
     private int EnemyPosition()
     {
         int enemyPos = (enemy.transform.position.x > transform.position.x) ?
             1 : -1;
     
         return enemyPos;
     }
 
     public bool IsGrounded()
     {
         bool grounded = GetComponent<BoxCollider2D>().
             IsTouchingLayers(LayerMask.GetMask("Ground"));
         return grounded;
     }
 }

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
0

Answer by luknight · Feb 11, 2020 at 06:13 PM

To me the issue would reside in your code somehow rewriting over your x velocity.

In this part of your script

  if (Mathf.Abs(jumpY) > Mathf.Epsilon)
  {
          rb.velocity = new Vector2(moveX * jumpX, jumpForce);
  }

Try to not use rb.velocity. Instead try to use rb.AddForce()

I'll do something like rb.AddForce(transform.up * jumpForce, ForceMode2D.Impulse)

If you want more explanation on how it works you can ask me for more details ( if it indeed does work )

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 the_spyor_ · Feb 11, 2020 at 06:16 PM

 @oraradaniel  //How I would do it is the code below
 
 
 if (Input.GetKeyDown(KeyCode.Space))
 
 
 //That's all I really know, I hope that kind of cleared it up, I make 3D games mainly, so I don't know how 2D would work
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.

Follow this Question

Answers Answers and Comments

156 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

Related Questions

I need help with fixing my "up special" mechanic. 1 Answer

Detect if player is left or right of enemy 1 Answer

I Can't Jump, And If I Remove if(isGrounded) I Can Do More Then 10 Jumps IN The Air 1 Answer

My player wont jump (2d game) 1 Answer

Raycast2D not working 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