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 /
This question was closed Apr 15, 2021 at 12:45 PM by i_cant_draw for the following reason:

Other

avatar image
0
Question by i_cant_draw · Mar 01, 2021 at 05:39 PM · unity 2d2d-platformerscripting beginner2d-physics

Handling friction in scripts for a 2D platformer

I've been working on a new 2D platformer project, as I have been working on one before, but I wanted to neaten up my code and also reinforce the different steps to making other objects. I just wanted some more practice for making 2D games. When creating the character, I had the issue of sticking to walls, which I was able to fix using a separate collider for the feet, and a separate collider for the body. I set the friction of the feet collider to 1, and the body collider to 0. They both have 0 bounciness. With these settings, however, I could not stop moving in midair. When letting go of the a and d keys (which are used to move) when I am jumping, I keep my momentum. Instead, I want my momentum to come to a stop. I tried creating a friction variable in my script, but this caused the player to continually slide, because the friction would keep being applied as long as the x velocity of the rigidbody is higher or lower than 0. With how the physics work, this causes the player to slide. Does anyone have a solution to this problem?

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

  • Sort: 
avatar image
0
Best Answer

Answer by benjaminfranklineatsfruit · Mar 03, 2021 at 03:40 PM

Try this:

  1. Use one collider, with a material that has no friction/bounce.

  2. Suppose "A" & "D" is your keys to move left and right.

  3. Use this code to "Stop".

      private Rigidbody2D rb2d;
             
         void Start()
         {
             rb2d = gameObject.GetComponent<Rigidbody2D>();
         }
         
         private void FixedUpdate()
         {
                 //assuming you use "d" & "a" to move left and right
                 //Stop moving/Skidding
                 if (Input.GetKeyUp("a") || Input.GetKeyUp("d"))
                 {
                      //set the x velocity to 0
                      rb2d.velocity = new Vector2(0, rb2d.velocity.y);
                 }
          }
    
    
    
    
    
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

Follow this Question

Answers Answers and Comments

141 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

Related Questions

OnTriggerEnter2D(Collider2D other) 2 Answers

Unity 2D player sticks on platform corners 2 Answers

How to implement sonic style Rolling/Spindash,How would I implement a rolling feature for the player in unity? 1 Answer

Way to achieve 3 lane mechanism in a 2D game 0 Answers

When Player Collide with Cube, Cube changes to Rigidbody 2D 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