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 DinoSore69 · Jun 13, 2021 at 08:17 AM · movement2d-platformerjumping

Unity Character Not Moving Midair

When on the ground my character moves fine, but the velocity gets locked in a certain direction whenever I jump.

public class Player : MonoBehaviour { [SerializeField] float moveSpeed = 5f; [SerializeField] float maxSpeed = 2f; [SerializeField] float jumpScalar = 250f;

 bool isOnGround;

 Rigidbody2D rb;

 private void Start()
 {
     rb = gameObject.GetComponent<Rigidbody2D>();
 }

 private void FixedUpdate()
 {
     Move();
     Jump();
 }

 private void Move()
 {
     if (Mathf.Abs(rb.velocity.x) < maxSpeed)
     {
         rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * moveSpeed, 0));
     }
 }

 private void Jump()
 {
     if (Input.GetButtonDown("Jump") && isOnGround)
     {
         Vector2 jumpForce = new Vector2(0, jumpScalar);
         rb.velocity = jumpForce;
     }
 }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     if (CollisionIsWithGround(collision))
     {
         isOnGround = true;
     }
 }

 private void OnCollisionExit2D(Collision2D collision)
 {
     if (!CollisionIsWithGround(collision))
     {
         isOnGround = false;
     }
 }

 private bool CollisionIsWithGround(Collision2D collision)
 {
     bool isWithGround = false;
     foreach (ContactPoint2D c in collision.contacts)
     {
         Vector2 collisionDirectionVector = c.point - rb.position;
         if (collisionDirectionVector.y < 0)
         {
             isWithGround = true;
         }
     }

     return isWithGround;
 }

}

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 DenisIsDenis · Jun 13, 2021 at 08:29 AM

There is no friction in the air, so the player doesn't slow down. Therefore, after exceeding the maximum speed, your condition in Move () will always return false.

Therefore, you need to check the speed after the application of force:

  private void Move()
  {
      rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * moveSpeed, 0));
      if (Mathf.Abs(rb.velocity.x) > maxSpeed)
      {
          rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * moveSpeed * (-1), 0));
      }
  }

So we cancel the force that increased the speed over the norm.


EDITED: @DinoSore69. I tested the script and modified it a bit to make it work:

     private void Move()
     {
         rb.AddForce(new Vector2(Input.GetAxis("Horizontal") * moveSpeed, 0));
 
         var xVel = rb.velocity.x;
         xVel = Mathf.Clamp(xVel, -maxSpeed, maxSpeed);
 
         rb.velocity = new Vector2(xVel, rb.velocity.y);
     }

I also fixed the Jump() function so that the player has a fixed jump speed:

     private void Jump()
     {
         if (Input.GetButton("Jump") && isOnGround)
         {
             rb.velocity = new Vector2(rb.velocity.x, jumpScalar);
         }
     }

And if you do not need the player to jump on the walls, then here is the modified script for defining isOnGround:

     private bool CollisionIsWithGround(Collision2D collision)
     {
         bool isWithGround = false;
         foreach (ContactPoint2D c in collision.contacts)
         {
             Vector2 collisionDirectionVector = c.point - rb.position;
 
             // Change "30" to the angle you want
             if (Vector2.Angle(collisionDirectionVector, Vector3.down) < 30)
             {
                 isWithGround = true;
             }
         }
         return isWithGround;
     }

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 DinoSore69 · Jun 14, 2021 at 04:13 AM 0
Share

Thanks for the reply @DenisIsDenis, I implemented the code, but it didn't change the result at all. I still have no player movement while in the air.

avatar image
0

Answer by liquidsolder · Jun 14, 2021 at 11:08 AM

your setting your jump x velocity to 0.

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

185 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

Related Questions

How to reduce speed while moving left and right in 2D platform 0 Answers

Problem between my player jump and player movement 1 Answer

Movement Script for Player... 1 Answer

Long press for charged Jump 1 Answer

How do I do a Snappy Jump in 2D with a 2D Jump Script? 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