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
0
Question by f0WL- · Apr 01, 2021 at 10:20 AM · movementrigidbodyphyics

Rigidbody Deceleration Uneven

I'm making a script to add player acceleration and deceleration. After struggling to get it working with a character controller, I finally switched to a rigidbody and everything works perfectly, except for deceleration. I want to be able to set the acceleration, max speed, and deceleration as variables so I can tweak them to figure out what feels best. This is my move function. It's called every fixed update by the server, and takes in _inputDirection, which is a Vector2 of the client's movement inputs. The player also has a Vector3 called speed, which is their x and z speed.

Vector3 _direction = Vector3.zero; _direction += transform.right * _inputDirection.x; _direction += transform.forward * _inputDirection.y; // X-Axis if (_direction.x > 0) { if (speed.x < 0) { speed.x += _direction.x * (acceleration + deceleration); } else { speed.x += _direction.x * acceleration; } } else if (_direction.x < 0) { if (speed.x > 0) { speed.x += _direction.x * (acceleration + deceleration); } else { speed.x += _direction.x * acceleration; } } else { if (speed.x > 0) { speed.x = Mathf.Max(speed.x - deceleration, 0); } else { speed.x = Mathf.Min(speed.x + deceleration, 0); } } // Z-Axis if (_direction.z > 0) { if (speed.z < 0) { speed.z += _direction.z * (acceleration + deceleration); } else { speed.z += _direction.z * acceleration; } } else if (_direction.z < 0) { if (speed.z > 0) { speed.z += _direction.z * (acceleration + deceleration); } else { speed.z += _direction.z * acceleration; } } else { if (speed.z > 0) { speed.z = Mathf.Max(speed.z - deceleration, 0); } else { speed.z = Mathf.Min(speed.z + deceleration, 0); } } if (speed.magnitude > maxSpeed) { speed = speed.normalized * maxSpeed; } rb.MovePosition(transform.position + speed * Time.deltaTime);

The player accelerates towards their max speed, they decelerate towards 0 if they stop moving, and if they start moving in a direction opposite the one they're currently moving, they both accelerate towards that new direction and decelerate from their old one. The issue is that if I turn diagonally, pick up speed, and stop moving, the player will start decelerating, but towards the end of their deceleration, they'll start moving another direction. I assume this is because if they're moving diagonally, their speeds in the x and z direction are uneven, so as each direction decelerates, one reaches zero while the other still has some speed, and the player starts only moving along the axis that still has speed.

I've tried multiplying the speed by something like .9 instead of subtracting, which fixes this issue. The only issue with this is the player's speed will keep getting smaller and smaller, but it never reaches 0. I've tried setting it to 0 if it's under a certain value, but this leads to the same issue with the deceleration being uneven towards the end. Any help getting either subtraction or division of player speed to work would be greatly appreciated.

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

0 Replies

· Add your reply
  • Sort: 

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

203 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

Related Questions

How to drag and drop a instantiate prefab on my mobile game 3D using touchs! 0 Answers

Ground clamping a rigidbody on slope 1 Answer

Make Rigidbody Character retain momentum 0 Answers

RigidBody.AddForce not working 1 Answer

Movement using rigidbody.velocity to apply a constant force until stop 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