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
1
Question by rubenflintstone · May 27, 2019 at 11:42 AM · 2d gamecharactercontrollerrigidbody2dvelocityvector2

How to stop cube from sliding (rg2d.velocity)

Hi all,

I'm currently busy with my charachter controller for my 2D game and like the smoothness from 'velocity' however i can't seem to solve the slipping from the charachter. Is there a solution for this problem?

My code:

       float moveH = Input.GetAxis("Horizontal");
       float moveV = Input.GetAxis("Vertical");
 
       if (moveH > 0)
         {
             rb2D.velocity = new Vector2(moveH * maxSpeed, rb2D.velocity.y);
         }
         else if (moveH < 0)
         {
             rb2D.velocity = new Vector2(moveH * maxSpeed, rb2D.velocity.y);
         }
         else
         {
             rb2D.velocity = new Vector2(0, rb2D.velocity.y);
         }
 
         if (moveV > 0)
         {
             rb2D.velocity = new Vector2(rb2D.velocity.x, moveV * maxSpeed);
         }
         else if (moveV < 0)
         {
             rb2D.velocity = new Vector2(rb2D.velocity.x, moveV * maxSpeed);
         }
         else
         {
             rb2D.velocity = new Vector2(rb2D.velocity.x, 0);
         }
 
         Debug.Log(moveH);
         Debug.Log(moveV);
     }



alt text

Kind regards,
Ruben

capture.png (48.8 kB)
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 Ran_Crump · May 27, 2019 at 05:06 PM

A quick and simple way would be to turn on Gravity Scale which will pull the object down to the colliders and slow it down due to collision. Another would be to create a Physics 2D Material and assign it in the Material Slot of the Rigid-body component in it you can control the amount of friction and bounciness the user experiences.

Comment
Add comment · Show 7 · 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 rubenflintstone · May 27, 2019 at 05:59 PM 0
Share

Hi @Ran_Crump, Thanks for your reply!

I'm creating a binding of isaac/enter the gungeon kind of game so the player won't really have physics as it needs to be able to walk up, down, left and right. I also created the 2d material for the rigid body but that doesn't seem to work either - will try some other things but friction doesn't seem to work as of now.

~Ruben

alt text (The last 1/0.5 seconds is when I released the input key, then the velocity is going down however I want it to stop immediately)

avatar image Ran_Crump rubenflintstone · May 27, 2019 at 08:21 PM 0
Share

Be sure you're applying the material to the rigidbody character and the physics material slot on the collider of the terrain

avatar image rubenflintstone Ran_Crump · May 28, 2019 at 12:30 PM 0
Share

I tried that but that sadly doesn't work. If I'm adding a collider on the 'background'/cube behind the player, the player/cube will glitch (because the player has a colider as well)

Show more comments

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

216 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

Related Questions

How do I make my character move on the Z axis on 2d rigidbody? 0 Answers

Rotating a velocity vector while keeping the magnitude 0 Answers

Why angle between these two velocities is 45? 2 Answers

JohnyControllerScript.cs(20,17): error CS0029: Cannot implicitly convert type `UnityEngine.Vector2' to `UnityEngine.Rigidbody2D' 0 Answers

Unity2D Check which direction the enemy AI is heading? 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