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 /
  • Help Room /
avatar image
0
Question by Bee3001 · Dec 27, 2018 at 01:40 AM · 2d2d platformer2d physics

2D Platformer smooth horizontal movement?

I'm very new to Unity and I'm trying to make a 2D platformer.

I'm currently using addforce for horizontal movement but I can't figure out a very minor problem. Hoping someone here could help me.

So with left and right movement, on the ground, its close to what I want where after moving say to the right for a while and the player gains some speed, if they switch to moving left they need to overcome a little momentum from when they were moving right before they start to move left. Kinda like how you would need a moment to skid to a halt.

My problem is, when the player is in the air, the same input this doesn't seem to be happening while the player is in the air (e.g when jumping) and I have no idea what I need to do to get it to act the same as it would if the player was on the ground.

Heres a quick visual of my problem (Sorry for the bad quality) where the top one is what I want when he turns (Starts going left then turns to go right) so theres a bit of resistance.

The bottom one is what I dont want, where he's just instantly moving at max speed the moment he turns around and this happens randomly when on the ground but all the time while in the air.

Heres my movement script which is in the Update() function:

         //Horizontal Movement
         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
 
         if (input.x != 0)
         {
             playerBody2D.AddForce(new Vector2(speed * input.x, 0));
 
             //Speed capping
             if (Mathf.Abs(playerBody2D.velocity.x) > maxSpeed)
             {
                 playerBody2D.velocity = new Vector2(maxSpeed * input.x, playerBody2D.velocity.y);
             }
         }
 
         //Flipping Sprite
         if (Input.GetAxis("Horizontal") > 0)
         {
             transform.localScale = new Vector3(1f, 1f, 1f);
             facingRight = true;
         }
 
         else if (Input.GetAxis("Horizontal") < 0)
         {
             transform.localScale = new Vector3(-1f, 1f, 1f);
             facingRight = false;
         }


I'm not great at coding so sorry if this is a simple noob problem and I'm just being dumb. I hope I've explained clearly.

Any help would be greatly appreciated, I'd been advised to use Physics and addforce which is what I've done here but if there are better ways of doing this, I would like to hear what suggestions people might have.

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 owen_unity55 · Dec 29, 2018 at 12:19 AM

In the first line of code try doing just Input.GetAxis instead of Input.GetAxisRaw. Not sure if it will work though, the part about it only working on the ground sometimes confuses me and I'm kind of new to Unity.

Good luck on the project and happy holidays!

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 Bee3001 · Dec 30, 2018 at 11:19 PM 0
Share

Thanks for your reply. Unfortunately, it didn't change anything. I tested GetAxis and GetAxisRaw with my controller and $$anonymous$$eyboard, GetAxisRaw would always return 1/-1 as intended which is the reason I wanted to use it but while GetAxis will return a variety of values between 0-1 for $$anonymous$$eyboard and xbox360 controller, my other controller still returns 1/-1 regardless of which one I'm using.

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

258 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

Related Questions

Cannot get my double jump to work (2D) , 0 Answers

Why is it when i make walls in unity 2d my sprite stays on the wall ? 0 Answers

[2D] Trying to make my character jump using addForce, but it's not working.,Rigidbody2d.AddForce isn't working. 0 Answers

Want to make a Pendulum between 2 characters 0 Answers

Tilemap Colliders not working with custom tile 3 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