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
1
Question by EvanCheddar · Apr 19, 2021 at 09:57 PM · 2d gamejumpingplatformermomentum

How to keep player momentum after a jump landing (2D)

Hey guys,

I'm creating a Mario clone and I need to keep the player's momentum into the jump and then onward into the landing. I can't figure out how to achieve this. Every time I land, the player has to build up momentum again. Any Idea how to fix this? Here's my code:

     Animator animator;
     Rigidbody2D rb;
     bool isGrounded;
     public float moveSpeed;
     public Vector2 acceleration;
     public float jumpHeight;
     public Transform groundCheckM;
     public Transform groundCheckL;
     public Transform groundCheckR;
 
     void Start()
     {
         animator = GetComponent<Animator>();
         rb = GetComponent<Rigidbody2D>();
         transform.eulerAngles = new Vector3(0, 0, 0);
     }
 
     private void Update()
     {
 
        if
              ((Physics2D.Linecast(transform.position,groundCheckM.position, 1 << LayerMask.NameToLayer("Floor/Platforms"))) || //Check if grounded
              (Physics2D.Linecast(transform.position, groundCheckL.position, 1 << LayerMask.NameToLayer("Floor/Platforms"))) ||
              (Physics2D.Linecast(transform.position, groundCheckR.position, 1 << LayerMask.NameToLayer("Floor/Platforms"))))
         {
             isGrounded = true;
             animator.SetBool("Jump", false);
         }
            
        else
         {
             isGrounded = false;
         } 
 
         animator.SetFloat("Walk", rb.velocity.x);  //Set animation float to x velocity 
        
         if (rb.velocity.x <= 0.03f && rb.velocity.x >= -0.03f && isGrounded) //Play "Idle" animation 
         {
             animator.Play("Mario_Idle");
         }
 
         if (rb.velocity.x >=4 || rb.velocity.x <=-4)
 
         {
             animator.speed = Mathf.Abs(rb.velocity.x / 5.5f); //Increase speed of walking animation with player's walking speed 
         }
 
     }
     void FixedUpdate()
     {
 
         if (Input.GetKey("d") || Input.GetKey("right")) //Move player to the right 
         {
             rb.AddForce(acceleration * rb.mass);
             transform.rotation = Quaternion.Euler (0, 0, 0);
         }
 
         else if (Input.GetKey("a") || Input.GetKey("left")) //Move player to the left
         {
             rb.AddForce(-acceleration * rb.mass);
             transform.rotation = Quaternion.Euler(0, 180, 0);
         }
 
             if (rb.velocity.x >= 10)
             {
                 rb.velocity = new Vector2(10, rb.velocity.y);   //Cap player speed at 10 when moving right
             }
 
         else if (rb.velocity.x <= -10)
             {
                 rb.velocity = new Vector2(-10, rb.velocity.y);  //Cap player speed at 10 when moving left 
         }
 
 
         if (Input.GetKey("space") && isGrounded) //Player jump 
         {
             rb.velocity += new Vector2(rb.velocity.x, jumpHeight);
             animator.SetBool("Jump", true);
         }
 
     }
 
 }
 
 

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 theDevGecko · Apr 23, 2021 at 04:46 PM

You could maybe take the x velocity right before the jump and put it in a variable then set the X velocity during the jump and after the jump to the initial velocity X .

   if (Input.GetKey("space") && isGrounded) //Player jump 
          {
              Vector2 InitalVelocity = rb.velocity; // you can make it a global variable so you can do this when landing too.
              rb.velocity += new Vector2(rb.velocity.x, jumpHeight);
              Vector2 vel = rb.velocity;
               vel.x = InitalVelocity.x;
               rb.velocity = vel;
              animator.SetBool("Jump", true);
          }






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

155 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

Related Questions

Character Instantly hits Ground after Falling off Object 1 Answer

How To Change Player As Soon as He Take Powerup 1 Answer

How can I change jump direction? 1 Answer

how to flip a sprite 0 Answers

problem with faux gravity (Small planet like 2D physics) and player jumping 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