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
2
Question by krixbro · Nov 04, 2021 at 02:07 PM · 2d gameunity 2drigidbody2dmovement script

How to make slippery/smooth movement in Unity 2D?

Hi, I would like to make a slippery 2D movement for my game i'm trying to make. I've got the movement for my player character, how do I make it slippery?

Here is my movement code.

 private const float MOVE_SPEED = 15f;
 

 private Rigidbody2D rigidbody2d;
 private Vector3 moveDir;

 private void Awake()
 {
     rigidbody2d = GetComponent<Rigidbody2D>();
 }

 private void Update()
 {
     // Basic Movement for the player

     float moveX = 0f;
     float moveY = 0f;

     if (Input.GetKey(KeyCode.W))
     {
         moveY = +1f;    
     }
     if (Input.GetKey(KeyCode.S))
     {
         moveY = -1f;
     }
     if (Input.GetKey(KeyCode.A))
     {
         moveX = -1f;
     }
     if (Input.GetKey(KeyCode.D))
     {
         moveX = +1f;
     }

     moveDir = new Vector3(moveX, moveY).normalized;
 }


 private void FixedUpdate()
 {
     rigidbody2d.velocity = moveDir * MOVE_SPEED;
 }

}

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by guubebra · Nov 04, 2021 at 06:07 PM

Hi! Try to use SmoothDamp (https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html) with the smoothTime at a high value.

For example:

 float smoothTime = .1f;
 float velocitySmoothing;
 
  private void FixedUpdate()
  {
      targetVelocity = moveDir * MOVE_SPEED;
      rigidbody2d.velocity = Time.deltaTime * Mathf.SmoothDamp(velocity, targetVelocity, ref velocitySmoothing, smoothTime);
  }
 
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
avatar image
0

Answer by krixbro · Nov 06, 2021 at 10:14 PM

Hi agian, this script works thank you so much @guubebra!

But how do you get that targetVelocity?

Cheers!

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 guubebra · Nov 08, 2021 at 01:13 PM 0
Share

just define it somewhere, it's only used in fixed update

avatar image
0

Answer by Abyssal_Epicc · Mar 17 at 09:39 PM

you can make it really slippery by just adding force to the rigidbody

 // For moving right, simplified version though, add whatever you please
 rigidbody2d.AddForce(Vector2.right);
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

184 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

Related Questions

Player is not moving 0 Answers

How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers

How to flip, on click a constant moving 2d rigidbody on the x-axis and maintain speed? 0 Answers

Why do GameObjects need Rigidbodies to collide? 0 Answers

How to use non-convex mesh collider with dynamic rigid body physics? 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